码迷,mamicode.com
首页 > 系统相关 > 详细

Linux之归档、压缩

时间:2015-04-11 20:57:27      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:linux归档   linux压缩   tar   zip   gzip   

 

Linux常用压缩工具:gzip、bzip、xz,这几个压缩工具不能压缩目录

 

-------------------------------------------------

 

gzip、bzip、xz的压缩比:xz>bzip>gzip

压缩比通常有1-9,9个级别,命令不指定压缩级别时,默认使用级别6;压缩比越大则越消耗cpu资源,压缩后的文件也约小,可使用参数,如:-4,指定压缩比级别;

 

gzip、bzip2、xz压缩包的后缀名:.gz、.bz2、.xz,例如:将文件test压缩后,压缩包名字为test.gz

 

gzip、bzip2、xz解压缩命令:gunzip = gzip -d、bunzip2 = bzip2 -d、unxz = xz –d

 

默认情况下,压缩文件后,源文件会被删除,bzip2、xz可以使用参数-k,达到压缩文件并不删除源文件的目的,gzip则可以通过输出重定向:gzip –c file > file.gz来达到目的

 

如果不解压,查看压缩包文件内容则使用以下命令

*.gz使用:zcat

*.bz2使用:bzcat

*.xz使用:xzcat

-------------------------------------------------

 

由于不能直接压缩目录,所以我们需要将目录归档(archive)成单个文件后,再进行压缩

 

Linux常用归档命令:tar

 

tar命令

    常用参数

    -c:创建归档

    -x:展开归档

    -f filename:指定归档后的文件名称,一般以*.tar作为文件名

    -t:不展开归档,查看归档文件列表

    -C:指定归档展开后的路径

    目录可以先归档成文件后,调用压缩工具进行压缩

    -z:调用gzip进行压缩

    -j:调用bzip2进行压缩

    -J:调用xz进行压缩

    --xattrs:归档后保留文件的扩展属性

[root@TESTHOST scripts]# cp -r /etc/init/ ./    #复制init目录到当前目录
[root@TESTHOST scripts]# ls
123  123.xz  init
[root@TESTHOST scripts]# tar -cf test.tar init    #将init目录归档为test.tar
[root@TESTHOST scripts]# ls
123  123.xz  init  test.tar
[root@TESTHOST scripts]# tar -tvf test.tar    #查看归档明细 
drwxr-xr-x root/root         0 2015-04-12 02:02 init/
-rw-r--r-- root/root       430 2015-04-12 02:02 init/rcS-emergency.conf
······    #省略
······
[root@TESTHOST scripts]# ls /tmp/
yum.log

- - - - - - - - - - - - - - - - - - -
[root@TESTHOST scripts]# tar -xf test.tar -C /tmp    #指定展开归档到/tmp目录下
[root@TESTHOST scripts]# ls /tmp/
init  yum.log

- - - - - - - - - - - - - - - - - - -

[root@TESTHOST scripts]# tar -Jcf test1.tar.xz init/r*    #归档并压缩
[root@TESTHOST scripts]# ls
123  123.xz  init  test1.tar.xz  test.tar
[root@TESTHOST scripts]# tar -Jxf test1.tar.xz -C /tmp    #解压并展开,此处的J可以省去,tar命令会根据压缩格式自动调用压缩工具进行解压
[root@TESTHOST scripts]# ls /tmp/init/
rc.conf  rcS.conf  rcS-emergency.conf  rcS-sulogin.conf

 

 

zip命令:同时归档、压缩,后缀名为.zip,解压缩命令为unzip,unzip -v能在不解压的情况下查看压缩包中文件明细

 

[root@TESTHOST scripts]# ls
123  123.xz  init
[root@TESTHOST scripts]# zip -r test.zip init   #如果要直接压缩目录,则需要-r进行递归操作,少了-r就目录写为init/*或这init/r*(目录中r开头的文件)
  adding: init/ (stored 0%)
  adding: init/rcS-emergency.conf (deflated 34%)
  adding: init/quit-plymouth.conf (deflated 37%)
  adding: init/start-ttys.conf (deflated 33%)
······    #省略
······
[root@TESTHOST scripts]# ls
123  123.xz  init  test.zip

- - - - - - - - - - - - - - - -

[root@TESTHOST scripts]# unzip -v test.zip    #unzip -v 能查在不解压的情况下查看压缩包文件明细
Archive:  test.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
       0  Stored        0   0% 04-12-2015 02:44 00000000  init/
     430  Defl:N      283  34% 04-12-2015 02:44 d32c7c59  init/rcS-emergency.conf
······    #省略
······
--------          -------  ---                            -------
    7946             4676  41%                            15 files
[root@TESTHOST scripts]#




本文出自 “Arvin Lau” 博客,请务必保留此出处http://64314491.blog.51cto.com/2784219/1631307

Linux之归档、压缩

标签:linux归档   linux压缩   tar   zip   gzip   

原文地址:http://64314491.blog.51cto.com/2784219/1631307

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!