Linux系統壓縮和解壓縮工具gzip、bzip2、xz以及tar打包工具總結

在Linux系統使用過程中,對于一些不是常用的文件,利用cpu的時間資源對文件進行壓縮可以節省一定的磁盤空間,對系統中某個目錄經常會有備份的需求,可以使用Linux系統中的tar打包工具實現,文件的壓縮對于大量的網絡文件傳輸可以節約帶寬,同時也能降低服務器的負載,我們經常需要從網站下載軟件包到本地主機,這些包基本都是打包壓縮的,要想使用安裝,需要我們解壓縮他們,掌握Linux系統中的壓縮解壓縮以及打包工具的使用是系統運維工程師基礎必備技能;

1)壓縮工具

2)打包工具

1.壓縮工具:

     (1)壓縮原理:

     這里簡單敘述一下,我們知道文本文件中有大量的重復字符串,單詞和文字對于單獨重復的字符串和單詞,我們可以使用一個標記符號代表,這樣只存儲一個標記字符;這只是一個大致的原理,當然不同的壓縮工具使用的算法也就不同,同時后面新出的壓縮算法更加優異合理,對CPU的資源占用也不是很高,實現快速壓縮;同時有個問題需要注意,壓縮和解壓縮都需要消耗大量的CPU時間資源,尤其是對大文件的操作,更需要考慮系統的負載狀況,所以對于系統中的文件壓縮要充分考慮清楚,有沒有必要浪費CPU的時間資源來換取節約磁盤空間,同時還有一點需要注意,對于二進制格式的數據流文件壓縮效果不是很好比如說MP3格式的音樂等,它們受到自身數據格式影響;

    (2)compress/uncompress:

    這是一種早期的Linux系統中的壓縮和解壓縮工具,壓縮比不是很好,所以很少在使用;壓縮后的文以.Z    結尾;

 

    (3)gzip/gunzip/zcat:

   

     壓縮:gzip

     語法:

       gzip [option] /path/to/file

    常見選項:

       -#:指定壓縮比,默認6,范圍(1-9),數字越大,壓縮比越高;

       -d:相當于命令gunzip,解壓文件;

       -c:默認情況下壓縮完文件后會把原文件刪除,如果想保留可以使用如下格式:

      gzip -c /path/to/sourcefile > /path/to/sourcefile.gz

      -r:可以遞歸對目錄下的所有文件進行壓縮;

    解壓縮:gunzip

    語法:

     gunzip [option] /path/to/file.gz

    常見選項:

         -r:可以遞歸對目錄下的所有壓縮文件進行解壓縮,默認刪除原壓縮文件;

    查看壓縮:zcat 正常情況下不能對壓縮文件進行查看,打開也是一堆亂碼;實際工作原理是:不解壓文                   件到磁盤,只是臨時解壓在內存中,提供壓縮文件查看, 

        語法:

     zcat /path/to/file

        *注意:使用查看壓縮文件命令時,尤其是大文件,解壓到內存中,是相當占內存


[root@xia7 dir]# ls -hl 
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# gzip messages 
[root@xia7 dir]# ls -hl
total 192K
-rw------- 1 root root 190K Aug 16 17:40 messages.gz
[root@xia7 dir]# gzip -d messages.gz 
[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# gzip -9 messages 
[root@xia7 dir]# ls -lh
total 188K
-rw------- 1 root root 187K Aug 16 17:40 messages.gz
[root@xia7 dir]# gunzip messages.gz 
[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# gzip -c messages >messages.gz
[root@xia7 dir]# ls -lh
total 1.6M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw-r--r-- 1 root root 190K Aug 18 10:30 messages.gz
[root@xia7 dir]# zcat messages.gz 
Aug 14 11:29:01 xia7 rsyslogd: [origin software="rsyslogd" swVersion="7.4.7" x-pid="819" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
Aug 14 11:30:01 xia7 systemd: Started Session 7 of user root.
Aug 14 11:30:01 xia7 systemd: Starting Session 7 of user root.
Aug 14 11:40:01 xia7 systemd: Started Session 8 of user root.
Aug 14 11:40:01 xia7 systemd: Starting Session 8 of user root.
Aug 14 11:50:01 xia7 systemd: Started Session 9 of user root.

(4)bzip2/bunzip2/bzcat:

 

     壓縮:bzip2

     語法:

     bzip2 [option] /path/to/file

     常見選項:

       -#:指定壓縮比,默認6,范圍(1-9),數字越大,壓縮比越高;

       -d:相當于命令bunzip2,解壓文件;

       -k:壓縮完后保留原文件;

 

      解壓縮:bunzip2

      語法:

             bunzip2 [option] /path/to/file.bz2

      常見選項: 

            -k:解壓縮完成后不刪除原壓縮文件;


     查看壓縮:bzcat

     語法:

            bzcat /path/to/file.bz2


     解壓縮:bunzip2 

     語法:

            bunzip [option] /path/to/file.bz2

    常見選項:

     -k:解壓縮完成后不刪除原壓縮文件;


[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# bzip2 messages 
[root@xia7 dir]# ls -lh
total 84K
-rw------- 1 root root 83K Aug 16 17:40 messages.bz2
[root@xia7 dir]# bzip2 -d  messages.bz2 
[root@xia7 dir]# ls -lh 
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# bzip2 -9 messages 
[root@xia7 dir]# ls -lh
total 84K
-rw------- 1 root root 83K Aug 16 17:40 messages.bz2
[root@xia7 dir]# bunzip2 messages.bz2 
[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# bzip2 -k messages 
[root@xia7 dir]# ls -lh
total 1.5M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw------- 1 root root  83K Aug 16 17:40 messages.bz2
[root@xia7 dir]# bzcat messages.bz2 
Aug 14 11:29:01 xia7 rsyslogd: [origin software="rsyslogd" swVersion="7.4.7" x-pid="819" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
Aug 14 11:30:01 xia7 systemd: Started Session 7 of user root.
Aug 14 11:30:01 xia7 systemd: Starting Session 7 of user root.
Aug 14 11:40:01 xia7 systemd: Started Session 8 of user root.
Aug 14 11:40:01 xia7 systemd: Starting Session 8 of user root.
Aug 14 11:50:01 xia7 systemd: Started Session 9 of user root.

(5)xz/unxz/xzcat:

   

    壓縮:xz

    語法:

        xz [option] /path/to/file

    常見選項:

        -#:指定壓縮比,默認6,范圍(1-9),數字越大,壓縮比越高;

        -d:相當于命令unxz,解壓文件;

        -k:壓縮完后保留原文件;

     

        查看壓縮:xzcat:

        語法:

            xzcat /path/to/file.xz

        

        解壓縮:unxz

        語法:

            unxz [option] /path/to/file.xz

        常見選項:

            -k:解壓縮完成后不刪除原壓縮文件;

[root@xia7 dir]# ll -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# xz messages 
[root@xia7 dir]# ll -lh
total 60K
-rw------- 1 root root 58K Aug 16 17:40 messages.xz
[root@xia7 dir]# xz -d messages.xz 
[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# xz -9 messages 
[root@xia7 dir]# ls -lh
total 60K
-rw------- 1 root root 58K Aug 16 17:40 messages.xz
[root@xia7 dir]# unxz messages.xz 
[root@xia7 dir]# ls -lh
total 1.4M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
[root@xia7 dir]# xz -k messages 
[root@xia7 dir]# ls -lh
total 1.5M
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw------- 1 root root  58K Aug 16 17:40 messages.xz
[root@xia7 dir]# xzcat messages.xz 
Aug 14 11:29:01 xia7 rsyslogd: [origin software="rsyslogd" swVersion="7.4.7" x-pid="819" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
Aug 14 11:30:01 xia7 systemd: Started Session 7 of user root.
Aug 14 11:30:01 xia7 systemd: Starting Session 7 of user root.
Aug 14 11:40:01 xia7 systemd: Started Session 8 of user root.
Aug 14 11:40:01 xia7 systemd: Starting Session 8 of user root.

  

(6)zip/unzip

     壓縮:zip

     語法:

     zip /path/to/file.zip /opt/to/file…

     zip壓縮工具可以把多個文件打包壓縮成一個文件;

        

        解壓縮:unzip

        語法:

            unzip /path/to/file.zip

[root@xia7 dir]# ll
total 1408
-rw-r--r-- 1 root root   13948 Aug 18 10:46 functions
-rw-r--r-- 1 root root      81 Aug 18 10:45 issue
-rw------- 1 root root 1415955 Aug 16 17:40 messages
-rw-r--r-- 1 root root    2016 Aug 18 10:45 passwd
[root@xia7 dir]# zip ceshi.zip ./*
  adding: functions (deflated 68%)
  adding: issue (deflated 2%)
  adding: messages (deflated 86%)
  adding: passwd (deflated 60%)
[root@xia7 dir]# ll -lh
total 1.6M
-rw-r--r-- 1 root root 196K Aug 18 10:48 ceshi.zip
-rw-r--r-- 1 root root  14K Aug 18 10:46 functions
-rw-r--r-- 1 root root   81 Aug 18 10:45 issue
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw-r--r-- 1 root root 2.0K Aug 18 10:45 passwd
[root@xia7 dir]# rm -f functions issue messages passwd 
[root@xia7 dir]# unzip ceshi.zip 
Archive:  ceshi.zip
  inflating: functions               
  inflating: issue                   
  inflating: messages                
  inflating: passwd                  
[root@xia7 dir]# ls
ceshi.zip  functions  issue  messages  passwd
[root@xia7 dir]#

2.打包工具:

         前面講到的眾多壓縮工具一般只能對文件壓縮,不能對整個目錄打包壓縮歸檔,tar

工具就能很好的實現既能打包歸檔同時又能壓縮;

 

(1)tar工具:

      命令:tar

      語法:

          tar [option]…/path/to/file.tar /path/to/file…

          tar [option]…/path/to/file.tar

          tar [option]…/path/to/file.tar.[gz|bz2|xz] /path/to/file…

          tar [option]…/path/to/file.tar.[gz|bz2|xz] 

          tar [option]…/path/to/file.tar.[gz|bz2|xz] [-C /path/to/dir]

     常見選項:

         -c:打包歸檔成一個文件,相當于備份;

         -x:解包歸檔文件,默認當前目錄;

         -C:解包到指定目錄;

         -t:查看打包歸檔中的文件名;

         -f:跟打包文件參數,一般其他命令使用要跟-f結合,注意-f要放到選項最后;

         -z:使用gzip壓縮和解壓縮;

         -j:使用bzip2壓縮和解壓縮;

         -J:使用xz壓縮和解壓縮;

[root@xia7 dir]# ll
total 1408
-rw-r--r-- 1 root root   13948 Aug 18 10:46 functions
-rw-r--r-- 1 root root      81 Aug 18 10:45 issue
-rw------- 1 root root 1415955 Aug 16 17:40 messages
-rw-r--r-- 1 root root    2016 Aug 18 10:45 passwd
[root@xia7 dir]# tar cf ceshi.tar ./*
[root@xia7 dir]# ll -lh
total 2.8M
-rw-r--r-- 1 root root 1.4M Aug 18 11:14 ceshi.tar
-rw-r--r-- 1 root root  14K Aug 18 10:46 functions
-rw-r--r-- 1 root root   81 Aug 18 10:45 issue
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw-r--r-- 1 root root 2.0K Aug 18 10:45 passwd
[root@xia7 dir]# tar zcf ceshi1.tar.gz functions issue messages 
[root@xia7 dir]# tar jcf ceshi2.tar.bz2 functions issue messages passwd 
[root@xia7 dir]# tar Jcf ceshi3.tar.xz functions issue messages passwd 
[root@xia7 dir]# ll -lh
total 3.1M
-rw-r--r-- 1 root root 194K Aug 18 11:15 ceshi1.tar.gz
-rw-r--r-- 1 root root  90K Aug 18 11:15 ceshi2.tar.bz2
-rw-r--r-- 1 root root  63K Aug 18 11:16 ceshi3.tar.xz
-rw-r--r-- 1 root root 1.4M Aug 18 11:14 ceshi.tar
-rw-r--r-- 1 root root  14K Aug 18 10:46 functions
-rw-r--r-- 1 root root   81 Aug 18 10:45 issue
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw-r--r-- 1 root root 2.0K Aug 18 10:45 passwd
[root@xia7 dir]# tar tf ceshi2.tar.bz2 
functions
issue
messages
passwd
[root@xia7 dir]# tar tf ceshi3.tar.xz 
functions
issue
messages
passwd
[root@xia7 dir]# rm -f functions issue messages passwd 
[root@xia7 dir]# ll -lh
total 1.8M
-rw-r--r-- 1 root root 194K Aug 18 11:15 ceshi1.tar.gz
-rw-r--r-- 1 root root  90K Aug 18 11:15 ceshi2.tar.bz2
-rw-r--r-- 1 root root  63K Aug 18 11:16 ceshi3.tar.xz
-rw-r--r-- 1 root root 1.4M Aug 18 11:14 ceshi.tar
[root@xia7 dir]# tar xf ceshi3.tar.xz 
[root@xia7 dir]# ll -lh
total 3.1M
-rw-r--r-- 1 root root 194K Aug 18 11:15 ceshi1.tar.gz
-rw-r--r-- 1 root root  90K Aug 18 11:15 ceshi2.tar.bz2
-rw-r--r-- 1 root root  63K Aug 18 11:16 ceshi3.tar.xz
-rw-r--r-- 1 root root 1.4M Aug 18 11:14 ceshi.tar
-rw-r--r-- 1 root root  14K Aug 18 10:46 functions
-rw-r--r-- 1 root root   81 Aug 18 10:45 issue
-rw------- 1 root root 1.4M Aug 16 17:40 messages
-rw-r--r-- 1 root root 2.0K Aug 18 10:45 passwd
[root@xia7 dir]# mkdir /tmp/ceshitar
[root@xia7 dir]# tar xf ceshi2.tar.bz2 -C /tmp/ceshitar/
[root@xia7 dir]# ll /tmp/ceshitar/
total 1408
-rw-r--r-- 1 root root   13948 Aug 18 10:46 functions
-rw-r--r-- 1 root root      81 Aug 18 10:45 issue
-rw------- 1 root root 1415955 Aug 16 17:40 messages
-rw-r--r-- 1 root root    2016 Aug 18 10:45 passwd
[root@xia7 dir]#

原創文章,作者:xiashixiang,如若轉載,請注明出處:http://www.www58058.com/37223

(0)
xiashixiangxiashixiang
上一篇 2016-08-18
下一篇 2016-08-18

相關推薦

  • JoSQL內存數據庫遠程代碼執行漏洞(含EXP)

    JoSQL全稱SQL for Java Objects,提供了應用SQL語句的Java對象的集合的能力開發,JoSQL提供了搜索,排序,group等對Java對象的集合進行類似SQL的查詢應該應用的功能。 例如,查找所有在2004年內修改過的html文件: SELECT * FROM   java.io.File WH…

    Linux干貨 2015-03-06
  • 文本及文件查找練習

    1、顯示當前系統上root、fedora或user1用戶的默認shell;     ~]# cat /etc/passwd | grep "^\(root\|fedora\|user1\)\>" | cut -d: -f1,7    root:/bin/…

    Linux干貨 2016-10-24
  • 網絡配置

    Internet 協議特征  運行于OSI 網絡層  面向無連接的協議  獨立處理數據包  分層編址  盡力而為傳輸  無數據恢復功能     IP地址 它們可唯一標識IP 網絡中的每臺設備 每臺主機(計算機、網絡設備、外圍設備)必須具有唯一的地址&…

    Linux干貨 2016-09-06
  • 磁盤管理及shell腳本編程練習

    1、創建一個10G分區,并格式為ext4文件系統 a)?要求其block大小為2048,預留空間百分比為2,卷標為MYDATA,默認掛載屬性包含acl; ~]# mke2fs –t ext4 –b 2048 –m 2 –L MYDATA? /dev/sda3 b)?掛載至/data/madata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳; ~…

    2017-11-15
  • 馬哥教育網絡班21期-第5周課程練習

    第5周課程練習 1、 顯示/boot/grub/grub.conf中以至少一個空白字符開頭的行; # grep "^[[:space:]]\+" /boot/grub/grub.conf 2、 顯示/etc/rc.d/rc.sysinit文件中以#開頭,后面跟至少一個空白字符,而后又有至少一個非空白字符的行; # egrep "…

    Linux干貨 2016-08-02
  • 邏輯卷管理器LVM

    一、LVM邏輯卷管理     lvm(Logical Volume Manage,邏輯卷管理)的重點在于可以彈性的調整filesystem的容量,而并非在于數據的存儲效率及安全上面。需要文件的讀寫效能或者是數據的可靠性是RAID所考慮的問題。LVM可以整合多個實體partition在一起,讓這些partitions看起…

    Linux干貨 2016-09-02
欧美性久久久久