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.www58058.com/53054

(0)
Net21-冰凍vs西瓜Net21-冰凍vs西瓜
上一篇 2016-12-05
下一篇 2016-12-05

相關推薦

  • haproxy 實戰之haproxy實現httpd負載均衡

    haproxy 實戰之haproxy實現httpd負載均衡 實驗目的haproxy + httpd實現負載均衡 1.準備機器,做好時間同步,域名主機名解析 192.168.42.151 [node2 haproxy] 192.168.42.152 [node3 httpd] 192.168.42.153 [node4 httpd] 2.node3,node4…

    Linux干貨 2017-06-29
  • 學習宣言

    已經虛度了第一個關鍵的10年,不能再失去下一個10年,3年內年薪20w,5年內年薪30w。

    Linux干貨 2016-12-26
  • keepalived+nginx部署(單主模型)

    環境準備Centos7系統,后端服務器提供web服務。地址規劃: VS1: 172.18.51.7 VS2:172.17.51.77 RS1:172.18.51.74 RS2:172.18.51.75 VirtualIP:172.18.51.82 拓撲圖: 原理:nginx是高度模塊化的應用程序,其中nginx_proxy模塊即可實現負載均衡,將前端的用戶請…

    Linux干貨 2017-05-17
  • 實現基于MYSQL驗證的vsftpd虛擬用戶

    馬哥教育面授21期 運維 vsftpd MySQL 說明:本實驗在兩臺CentOS主機上實現,一臺做為FTP服務器,一臺做數據庫服務器 一、安裝所需要包和包組: 在數據庫服務器上安裝包: yum –y install mariadb-server mariadb-devel systemctl start mariad…

    Linux干貨 2016-12-21
  • https介紹

    什么是https HTTP協議傳輸的數據都是未加密的,也就是明文的,因此使用HTTP協議傳輸隱私信息非常不安全。為了保證這些隱私數據能加密傳輸,于是網景公司設計了SSL(Secure Sockets Layer)協議用于對HTTP協議傳輸的數據進行加密,從而就誕生了HTTPS。SSL目前的版本是3.0,被IETF(Internet Engineering T…

    Linux干貨 2017-12-04
  • awk的詳細用法

    awk簡介 awk是一種編程語言,也可以說是一個腳本語言解釋器,主要是用來實現文本的格式化輸出。awk是取了其三位作者名字的首字母,即Aho,Weinberger和Kernighan。awk在Linux上是基于GNU協議實現的,即GNU awk,也就是gawk。 awk的工作模式 遍歷整個文件,從第一行到最后一行。每次僅讀取文件的一行,依照用戶指定的輸入分隔…

    Linux干貨 2016-11-14
欧美性久久久久