Linux下常用压缩工具总结

gzip:

1)只能针对普通文件进行压缩,对文件夹、符号链接无效。

2)如果想对多个文件一起压缩并打包,gzip是无法办到的,需要结合tar进行

[root@mysql-master databackup]# ll
总用量 32
drwx------ 2 root root 4096 10月 12 00:43 2016-10-12_00-43-29
drwx------ 2 root root 4096 10月 12 00:44 2016-10-12_00-44-00
drwx------ 2 root root 4096 10月 12 00:46 2016-10-12_00-46-53
drwx------ 2 root root 4096 10月 12 00:47 2016-10-12_00-47-43
drwx------ 2 root root 4096 10月 12 00:49 2016-10-12_00-49-02
-rw-r--r-- 1 root root 2522 10月 19 21:17 passwd
drwxr-xr-x 2 root root 4096 10月 12 00:37 xtrabackup
drwxr-xr-x 2 root root 4096 10月 12 00:37 xtrabackuplog
使用“-c”选项,可以保留原有文件
[root@mysql-master databackup]# gzip -c passwd >passwd.gz
[root@mysql-master databackup]# ll
总用量 36
drwx------ 2 root root 4096 10月 12 00:43 2016-10-12_00-43-29
drwx------ 2 root root 4096 10月 12 00:44 2016-10-12_00-44-00
drwx------ 2 root root 4096 10月 12 00:46 2016-10-12_00-46-53
drwx------ 2 root root 4096 10月 12 00:47 2016-10-12_00-47-43
drwx------ 2 root root 4096 10月 12 00:49 2016-10-12_00-49-02
-rw-r--r-- 1 root root 2522 10月 19 21:17 passwd
-rw-r--r-- 1 root root 1006 10月 19 21:18 passwd.gz
drwxr-xr-x 2 root root 4096 10月 12 00:37 xtrabackup
drwxr-xr-x 2 root root 4096 10月 12 00:37 xtrabackuplog
直接使用“gzip+文件”的压缩方式,原始文件不会被保留
[root@mysql-master databackup]# cp /etc/shadow .
[root@mysql-master databackup]# ll
总用量 40
drwx------ 2 root root 4096 10月 12 00:43 2016-10-12_00-43-29
drwx------ 2 root root 4096 10月 12 00:44 2016-10-12_00-44-00
drwx------ 2 root root 4096 10月 12 00:46 2016-10-12_00-46-53
drwx------ 2 root root 4096 10月 12 00:47 2016-10-12_00-47-43
drwx------ 2 root root 4096 10月 12 00:49 2016-10-12_00-49-02
-rw-r--r-- 1 root root 2522 10月 19 21:17 passwd
-rw-r--r-- 1 root root 1006 10月 19 21:18 passwd.gz
---------- 1 root root 1393 10月 19 21:19 shadow
drwxr-xr-x 2 root root 4096 10月 12 00:37 xtrabackup
drwxr-xr-x 2 root root 4096 10月 12 00:37 xtrabackuplog
[root@mysql-master databackup]# gzip shadow 
[root@mysql-master databackup]# ll
总用量 40
drwx------ 2 root root 4096 10月 12 00:43 2016-10-12_00-43-29
drwx------ 2 root root 4096 10月 12 00:44 2016-10-12_00-44-00
drwx------ 2 root root 4096 10月 12 00:46 2016-10-12_00-46-53
drwx------ 2 root root 4096 10月 12 00:47 2016-10-12_00-47-43
drwx------ 2 root root 4096 10月 12 00:49 2016-10-12_00-49-02
-rw-r--r-- 1 root root 2522 10月 19 21:17 passwd
-rw-r--r-- 1 root root 1006 10月 19 21:18 passwd.gz
---------- 1 root root  536 10月 19 21:19 shadow.gz
drwxr-xr-x 2 root root 4096 10月 12 00:37 xtrabackup
drwxr-xr-x 2 root root 4096 10月 12 00:37 xtrabackuplog
使用“gzip -d 压缩文件”,进行解压
[root@mysql-master databackup]# gzip -d shadow.gz
[root@mysql-master databackup]# ll
总用量 40
drwx------ 2 root root 4096 10月 12 00:43 2016-10-12_00-43-29
drwx------ 2 root root 4096 10月 12 00:44 2016-10-12_00-44-00
drwx------ 2 root root 4096 10月 12 00:46 2016-10-12_00-46-53
drwx------ 2 root root 4096 10月 12 00:47 2016-10-12_00-47-43
drwx------ 2 root root 4096 10月 12 00:49 2016-10-12_00-49-02
-rw-r--r-- 1 root root 2522 10月 19 21:17 passwd
-rw-r--r-- 1 root root 1006 10月 19 21:18 passwd.gz
---------- 1 root root 1393 10月 19 21:19 shadow
drwxr-xr-x 2 root root 4096 10月 12 00:37 xtrabackup
drwxr-xr-x 2 root root 4096 10月 12 00:37 xtrabackuplog
如上,我们发现原有的shadow文件已经出来了
gzip有九个压缩级别1-9(数字越大,压缩强度越高,速度对应的也会慢些),默认级别为6
# gzip -1 test.log

tar:

压缩/etc文件:
[root@mysql-master databackup]# tar -czvf etc.tar.gz /etc
[root@mysql-master databackup]# du -sh etc.tar.gz 
12Metc.tar.gz
解压缩,并进行查看
[root@mysql-master databackup]# tar -xf etc.tar.gz
tar常用的几个选项:
-c选项:表示要进行打包动作
-x选项:表示要进行拆包动作
-z选项:表示用gzip进行压缩或解压缩
-v选项:表示在拆包过程中直接整个过程,把已拆包的文件显示出来
-f选项:表示指定要拆包的文件
-t选项:列出打包的内容,适用于不想解压,但想查看压缩包内容的情况
[root@mysql-master databackup]# du -sh src.tar.gz 
607Msrc.tar.gz
[root@mysql-master databackup]# tar -ztvf src.tar.gz |less

bzip2:

bzip2用于压缩文件,bunzip2用于解压文件

但是我们查看链接文件,会发现,其实bunzip2其实就是bzip2的一个链接

[root@mysql-master ~]# ls -l /usr/bin/bunzip2 
lrwxrwxrwx. 1 root root 5 7月   5 23:42 /usr/bin/bunzip2 -> bzip2
[root@mysql-master ~]# bzip2 install.log
[root@mysql-master ~]# ls
anaconda-ks.cfg        index.html       install.log.syslog  M_pass.log  R_Server.log  模板  图片  下载  桌面
changehostpassword.sh  install.log.bz2  ip_list.txt         R_PWD.txt   公共的        视频  文档  音乐
[root@mysql-master ~]# file install.log.bz2 
install.log.bz2: bzip2 compressed data, block size = 900k
[root@mysql-master ~]# bunzip2 -d install.log.bz2 
[root@mysql-master ~]# ls
anaconda-ks.cfg        index.html   install.log.syslog  M_pass.log  R_Server.log  模板  图片  下载  桌面
changehostpassword.sh  install.log  ip_list.txt         R_PWD.txt   公共的        视频  文档  音乐

zip:

与gzip/bzip2类似,zip用于文件压缩,unzip命令用于解压缩,zip支持对文件和文件夹的压缩,-r表示递归

注意:zip在压缩文件的时候,会保留原文件。

[root@mysql-master ~]# cd /usr/local/src/
[root@mysql-master src]# ls
cmake-2.8.8         libiconv-1.14.tar.gz    mcrypt-2.6.8         mhash-0.9.9.9.tar.gz                 mysql-5.5.32.tar.gz  php-5.3.27
cmake-2.8.8.tar.gz  libmcrypt-2.5.8         mcrypt-2.6.8.tar.gz  mysql-5.5.32                         nginx-1.6.2          php-5.3.27.tar.gz
libiconv-1.14       libmcrypt-2.5.8.tar.gz  mhash-0.9.9.9        mysql-5.5.32-linux2.6-x86_64.tar.gz  nginx-1.6.2.tar.gz
[root@mysql-master src]# zip -r nginx.zip nginx-1.6.2.tar.gz nginx-1.6.2/
[root@mysql-master src]# ls
cmake-2.8.8         libiconv-1.14.tar.gz    mcrypt-2.6.8         mhash-0.9.9.9.tar.gz                 mysql-5.5.32.tar.gz  nginx.zip
cmake-2.8.8.tar.gz  libmcrypt-2.5.8         mcrypt-2.6.8.tar.gz  mysql-5.5.32                         nginx-1.6.2          php-5.3.27
libiconv-1.14       libmcrypt-2.5.8.tar.gz  mhash-0.9.9.9        mysql-5.5.32-linux2.6-x86_64.tar.gz  nginx-1.6.2.tar.gz   php-5.3.27.tar.gz
[root@mysql-master src]# du -sh nginx.zip 
7.7Mnginx.zip

unzip结合-d参数,解压到指定文件夹

[root@mysql-master src]# mkdir -p /mysqlbackup
[root@mysql-master src]# unzip -d /mysqlbackup nginx.zip 
[root@mysql-master src]# ls -l /mysqlbackup/
总用量 792
drwxr-xr-x 9 root root   4096 9月  16 2014 nginx-1.6.2
-rw-r--r-- 1 root root 804164 6月  22 01:17 nginx-1.6.2.tar.gz

使用“unzip -v 压缩文件”的组合,不解压的情况下,来查看压缩文件中的内容

[root@mysql-master src]# unzip -v nginx.zip 
Archive:  nginx.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
  804164  Defl:N   804176   0% 06-22-2016 01:17 c721c245  nginx-1.6.2.tar.gz
       0  Stored        0   0% 09-16-2014 20:23 00000000  nginx-1.6.2/
    2369  Defl:N      732  69% 09-16-2014 20:23 85d03809  nginx-1.6.2/configure
       0  Stored        0   0% 09-22-2016 15:46 00000000  nginx-1.6.2/contrib/
       0  Stored        0   0% 09-16-2014 20:23 00000000  nginx-1.6.2/contrib/vim/
       0  Stored        0   0% 09-22-2016 15:46 00000000  nginx-1.6.2/contrib/vim/indent/
     250  Defl:N      165  34% 09-16-2014 20:23 87c1f91b  nginx-1.6.2/contrib/vim/indent/nginx.vim
       0  Stored        0   0% 09-22-2016 15:46 00000000  nginx-1.6.2/contrib/vim/ftdetect/
     198  Defl:N       79  60% 09-16-2014 20:23 68d244f2  nginx-1.6.2/contrib/vim/ftdetect/nginx.vim
       0  Stored        0   0% 09-16-2014 20:23 00000000  nginx-1.6.2/contrib/vim/syntax/
   31641  Defl:N     5987  81% 09-16-2014 20:23 fe4d7202  nginx-1.6.2/contrib/vim/syntax/nginx.vim
       0  Stored        0   0% 09-22-2016 15:46 00000000  nginx-1.6.2/contrib/unicode2nginx/

使用“unzip -t 压缩文件”,验证压缩文件的完整性

[root@mysql-master src]# unzip -t nginx.zip 
Archive:  nginx.zip
    testing: nginx-1.6.2.tar.gz       OK
    testing: nginx-1.6.2/             OK
    testing: nginx-1.6.2/configure    OK
    testing: nginx-1.6.2/contrib/     OK
    testing: nginx-1.6.2/contrib/vim/   OK
    testing: nginx-1.6.2/contrib/vim/indent/   OK
    testing: nginx-1.6.2/contrib/vim/indent/nginx.vim   OK
    testing: nginx-1.6.2/contrib/vim/ftdetect/   OK
    testing: nginx-1.6.2/contrib/vim/ftdetect/nginx.vim   OK
    testing: nginx-1.6.2/contrib/vim/syntax/   OK
    testing: nginx-1.6.2/contrib/vim/syntax/nginx.vim   OK
    testing: nginx-1.6.2/contrib/unicode2nginx/   OK
    testing: nginx-1.6.2/contrib/unicode2nginx/koi-utf   OK
    -------------------------------------------------------
    -------------------------------------------------------
    testing: nginx-1.6.2/Makefile     OK
    testing: nginx-1.6.2/conf/        OK
    testing: nginx-1.6.2/conf/uwsgi_params   OK
    testing: nginx-1.6.2/conf/koi-utf   OK
    testing: nginx-1.6.2/conf/win-utf   OK
    testing: nginx-1.6.2/conf/mime.types   OK
    testing: nginx-1.6.2/conf/fastcgi.conf   OK
    testing: nginx-1.6.2/conf/koi-win   OK
    testing: nginx-1.6.2/conf/fastcgi_params   OK
    testing: nginx-1.6.2/conf/scgi_params   OK
    testing: nginx-1.6.2/conf/nginx.conf   OK
No errors detected in compressed data of nginx.zip.

zip的扩展:

当我们配置好压缩文件的时候,发现其中某个比较占空间的文件并不是我们想要的,并且,我们不想重新解压和压缩。

向压缩文件中删除文件:
[root@mysql-master src]# zip nginx.zip -d nginx-1.6.2.tar.gz 
deleting: nginx-1.6.2.tar.gz
[root@mysql-master src]# unzip -v nginx.zip |grep nginx-1.6.2.tar.gz 
发现没有该文件存在了
向压缩文件中添加文件:
[root@mysql-master src]# zip -m nginx.zip nginx-1.6.2.tar.gz 
  adding: nginx-1.6.2.tar.gz (deflated 0%)
[root@mysql-master src]# unzip -v nginx.zip |grep nginx-1.6.2.tar.gz
  804164  Defl:N   804176   0% 06-22-2016 01:17 c721c245  nginx-1.6.2.tar.gz

原创文章,作者:Net21-冰冻vs西瓜,如若转载,请注明出处:http://www.178linux.com/53054

(0)
Net21-冰冻vs西瓜Net21-冰冻vs西瓜
上一篇 2016-12-05 17:04
下一篇 2016-12-05 17:04

相关推荐

  • 脚本练习

    脚本练习: 1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。 #!/bin/bash #功能:编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,…

    Linux干货 2016-08-12
  • 通过虚拟机制作RAID5

    制作RAID5的过程,包括制作RAID5的制作,增加和删除RAID

    2017-12-10
  • 马哥教育网络班22期+第7周课程练习

    week7: 1、创建一个10G分区,并格式为ext4文件系统;    (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;    (2) 挂载至/data/mydata目录,要求挂载时禁止程…

    Linux干货 2016-09-26
  • 虽千万人吾往矣

    哈佛有句名言 “当你觉得为时已晚的时候,恰恰是最早的时候”。这句话给了我虽千万人吾往矣的勇气。 之前那份工作懒散 安逸 。当我下定决心从那种环境中走出来的时候还是下了很大的决心。陌生的行业 陌生的环境 陌生的前途。我抱着对这份行业无限的憧憬来到了北京。   作为一个南方人的确有点不适合北方这黄沙漫漫的城市,刚来的一两天不停的打喷嚏,可能这就是北京欢…

    Linux干货 2018-03-26
  • 小白易患错误之绝对路径和相对路径的操作错误

    小白易患错误之绝对路径和相对路径的操作错误 作为一个不安稳的小白,一天都在那路乱折腾,恰巧,老师课程题目中有一题将/etc/skel 这个目录的文件除了..和. 复制到/home/USRNAEM 的家目录下。然后自以为是不按照老师的方法,自己折腾用了这样一条命令 [root@local skel]# ls -A .bash_lo…

    Linux干货 2016-08-05
  • Linux用户和组命令

                        groupadd命令用户和工作组管理 groupadd命令用于创建一个新的工作组,新工作组的信息将被添加到系统文件中。-g:指定新建工作组的id;  -r:创建系统工作组,系统工作组的组ID小于500; -K:…

    2016-02-09