歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux壓縮與歸檔工具詳解

Linux壓縮與歸檔工具詳解

日期:2017/2/28 13:49:33   编辑:Linux教程

壓縮出現的原因簡析

壓縮為什麼會誕生?我們可以想象一下如果沒有壓縮的場景:磁盤不夠用了,再買一塊擴展;一個游戲10G,下載下來得一整天,我們要上傳一個1G電影得1天...買一塊硬盤好貴啊,上傳下載好耗費時間啊。如果我們可以將游戲壓縮到原來的80%,我們就可以節省下來20%的磁盤空間,同時也節省了下載及上傳的時耗。所以壓縮誕生的目的首先是解決磁盤空間問題,其次也是為了節省我們上傳下載的時耗問題。

看起來它的出現真的是好的無以復加,不過它也有它的不足,壓縮必定伴隨著壓縮這個過程,同樣若是想要解壓也需要解壓這個過程,這個過程需要占用大量CPU的時間,我們應該會有這種感受,當我們壓縮或解壓縮大文件時,個人PC會很卡,因為當前的較多CPU資源都用於處理壓縮或解壓這個進程。

所以壓縮的出現是與現實的妥協與折中的產物,我們節省了磁盤空間,減少了上傳下載時耗,但我們卻會在解壓或壓縮時增加了CPU的負擔。

實驗環境為CentOS7.2


壓縮及解壓工具(包括歸檔工具tar及cpio)

下面我們就來了解下Linux中的壓縮解壓縮及歸檔工具

它主要有如下幾種:

compress/uncompress: .Z 很久之前使用的一款壓縮與解壓工具,現在幾乎消失蹤跡(本文不予介紹)

gzip/gunzip: .gz 現在Linux系統中使用范圍最廣的壓縮與解壓工具

bzip2/bunzip2: .bz2 在gzip之後出現,因其壓縮比並沒有明顯提升,因此使用的並不太多

xz/unxz: .xz 最近出現的壓縮與解壓工具,因其壓縮比較之gzip有了明顯的提高,因此近來其用人數越來越多,興許它可以取代gzip的地位

zip/unzip 這款壓縮與解壓工具的兼容性更好,它的平台很廣,Unix,Windows,Linux,Mac等都可以使用。

tar, cpio 此為歸檔工具,tar用的最廣,cpio一般不用(這裡不予介紹)

…………………………………………………………………………………………………………

gzip/gunzip/zcat

gzip、gunzip、zcat:compress or expand files

注:默認壓縮後刪除原文件

命令使用格式

gzip [OPTION]... FILE ...

常用選項

-d: 解壓縮,相當於gunzip

-c: 將結果輸出至標准輸出;壓縮後保留原文件

其格式為:gzip -C FILE > /PATH/TO/SOMEFILE.gz

-#:1-9,指定壓縮比,數字越大壓縮比越大;默認為6,通常不建議指定壓縮比。

zcat:不顯式展開的前提下查看文本文件內容;

…………………………………………………………………………………………………………

命令演示

我們把/var/log/message文件拷貝到/test文件夾內

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 [root@localhost test]# ll -h 總用量 2.5M -rw-------. 1 root root 2.5M 8月 19 16:33 messages [root@localhost test]# gzip messages [root@localhost test]# ll -h 總用量 196K -rw-------. 1 root root 194K 8月 19 16:33 messages.gz [root@localhost test]# zcat messages.gz Aug 14 11:39:15 centos7 avahi-daemon[905]: Invalid response packet from host 10.1.252. 7. Aug 14 11:39:16 centos7 avahi-daemon[905]: Invalid response packet from host 10.1.252. 7. ... [root@localhost test]# gzip -d messages.gz [root@localhost test]# ll -h 總用量 2.5M -rw-------. 1 root root 2.5M 8月 19 16:33 messages

上面演示了壓縮,zcat,解壓的過程,這個過程還說明了在壓縮文件後會默認將原文件刪除,並且解壓後自動會為原文件加上.gz後綴,本來文件後綴對Linux沒有意義,但是壓縮是個例外

下面再演示下指定壓縮比及壓縮後保留原文件的操作。

1 2 3 4 5 [root@localhost test]# gzip -9 -c messages > /test/message1.gz [root@localhost test]# ll -h 總用量 2.7M -rw-r--r--. 1 root root 188K 8月 19 16:52 message1.gz -rw-------. 1 root root 2.5M 8月 19 16:33 messages

對比兩個實驗結果可以發現大的壓縮比確實可以減少占的磁盤空間,但是默認的6與指定的9相差並不大,因此實際工作中一般不指定文件的壓縮比。

…………………………………………………………………………………………………………

bzip2/bunzip2/bzcat

命令使用格式

bzip2 [OPTION]... FILE ...

常用選項

-k: keep, 保留原文件;其bzip -k FILE即可

-d:解壓縮,相當於bzip2

-#:1-9,指定壓縮比,數字越大壓縮比越大;默認為6,通常不建議指定壓縮比。

bzcat:不顯式展開的前提下查看文本文件內容;

…………………………………………………………………………………………………………

命令演示

1 2 3 4 5 [root@localhost test]# bzip2 messages [root@localhost test]# ll -h 總用量 316K -rw-r--r--. 1 root root 188K 8月 19 16:52 message1.gz -rw-------. 1 root root 128K 8月 19 16:33 messages.bz2

可以看出來bzip2確實比gzip的壓縮比好。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 [root@localhost test]# bzip2 -d messages.bz2 [root@localhost test]# ll -h 總用量 2.7M -rw-r--r--. 1 root root 188K 8月 19 16:52 message1.gz -rw-------. 1 root root 2.5M 8月 19 16:33 messages [root@localhost test]# bzip2 -k messages [root@localhost test]# ll -h 總用量 2.8M -rw-r--r--. 1 root root 188K 8月 19 16:52 message1.gz -rw-------. 1 root root 2.5M 8月 19 16:33 messages -rw-------. 1 root root 128K 8月 19 16:33 messages.bz2 [root@localhost test]# bzcat messages.bz2 | more Aug 14 11:39:15 centos7 avahi-daemon[905]: Invalid response packet from host 10.1.252. 7. Aug 14 11:39:16 centos7 avahi-daemon[905]: Invalid response packet from host 10.1.252. 7. ...

上面為bzip2的解壓、留原文件的壓縮與bzcat。

…………………………………………………………………………………………………………

xz/unxz/xzcat

命令使用格式

xz [OPTION]... FILE ...

常用選項

-k: keep, 保留原文件;表達格式為:xz -k FILE

-d:解壓縮,相當於unxz

-#:1-9,指定壓縮比,數字越大壓縮比越大;默認為6,通常不建議指定壓縮比。

xzcat: 不顯式展開的前提下查看文本文件內容;

…………………………………………………………………………………………………………

命令演示

1 2 3 4 5 6 [root@localhost test]# xz messages [root@localhost test]# ll -h 總用量 408K -rw-r--r--. 1 root root 188K 8月 19 16:52 message1.gz -rw-------. 1 root root 128K 8月 19 16:33 messages.bz2 -rw-------. 1 root root 90K 8月 19 16:33 messages.xz

從上面三種壓縮工具壓縮相同的文件可以明顯看到xz的壓縮效果最好

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 [root@localhost test]# xz -d messages.xz [root@localhost test]# ll -h 總用量 2.8M -rw-r--r--. 1 root root 188K 8月 19 16:52 message1.gz -rw-------. 1 root root 2.5M 8月 19 16:33 messages -rw-------. 1 root root 128K 8月 19 16:33 messages.bz2 [root@localhost test]# xz -k messages [root@localhost test]# ll -h 總用量 2.9M -rw-r--r--. 1 root root 188K 8月 19 16:52 message1.gz -rw-------. 1 root root 2.5M 8月 19 16:33 messages -rw-------. 1 root root 128K 8月 19 16:33 messages.bz2 -rw-------. 1 root root 90K 8月 19 16:33 messages.xz [root@localhost test]# xzcat messages.xz | more Aug 14 11:39:15 centos7 avahi-daemon[905]: Invalid response packet from host 10.1.252. 7. ...

…………………………………………………………………………………………………………

tar Linux中最常用的歸檔工具

命令使用格式:

tar [OPTION]...

(1) 創建歸檔

tar -c -f /PATH/TO/SOMEFILE.tar FILE...

tar -cf /PATH/TO/SOMEFILE.tar FILE...

(2) 查看歸檔文件中的文件列表

tar -t -f /PATH/TO/SOMEFILE.tar

(3) 展開歸檔

tar -x -f /PATH/TO/SOMEFILE.tar

tar -x -f /PATH/TO/SOMEFILE.tar -C /PATH/TO/DIR

4)結合壓縮工具實現:歸檔並壓縮

-j: bzip2,

-jcf 使用bzip2對目標文件夾進行歸檔並以bzip2格式壓縮

-jxf 使用bzip2對.bx2後綴的tar包進行解壓並展開歸檔

-z: gzip,

-zcf使用gzip對目標文件夾進行歸檔並以gzip格式壓縮

-zxf使用gzip對.gz後綴的tar包進行解壓並展開歸檔

-J: xz

-Jcf使用xz對目標文件夾進行歸檔並以xz格式壓縮

-Jxf使用xz對.xz後綴的tar包進行解壓並展開歸檔

…………………………………………………………………………………………………………

命令演示

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [root@localhost test]# cp /var/log/messages* ./ [root@localhost test]# ll -h 總用量 16M -rw-r--r--. 1 root root 188K 8月 19 16:52 message1.gz -rw-------. 1 root root 2.5M 8月 19 17:14 messages -rw-------. 1 root root 1.1M 8月 19 17:14 messages-20160724 -rw-------. 1 root root 1.4M 8月 19 17:14 messages-20160731 -rw-------. 1 root root 4.4M 8月 19 17:14 messages-20160807 -rw-------. 1 root root 6.1M 8月 19 17:14 messages-20160814 -rw-------. 1 root root 128K 8月 19 16:33 messages.bz2 -rw-------. 1 root root 90K 8月 19 16:33 messages.xz [root@localhost test]# mkdir test [root@localhost test]# mv message* test/ [root@localhost test]# ll 總用量 4 drwxr-xr-x. 2 root root 4096 8月 19 17:14 test

先從/var/log/message文件夾下拷貝一些文件,之後在原路徑下創建一新文件夾再將這些文件轉移其中

下面開始歸檔操作

1 2 3 4 5 [root@localhost test]# tar -cf /test/message1.tar test/ [root@localhost test]# ll -h 總用量 16M -rw-r--r--. 1 root root 16M 8月 19 17:16 message1.tar drwxr-xr-x. 2 root root 4.0K 8月 19 17:14 test

歸檔後總大小並沒有減小,通常反而會大一點,所以一般在歸檔後進行壓縮。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@localhost test]# tar -t -f /test/message1.tar test/ test/message1.gz test/messages test/messages-20160724 test/messages-20160731 test/messages-20160807 test/messages-20160814 test/messages.bz2 test/messages.xz [root@localhost test]# xz message1.tar [root@localhost test]# ll -h 總用量 896K -rw-r--r--. 1 root root 890K 8月 19 17:16 message1.tar.xz drwxr-xr-x. 2 root root 4.0K 8月 19 17:14 test

這裡16M文件被xz工具壓縮至890K。

1 2 3 4 5 6 7 8 9 [root@localhost test]# rm -rf test/ [root@localhost test]# ll 總用量 892 -rw-r--r--. 1 root root 910960 8月 19 17:16 message1.tar.xz [root@localhost test]# tar -xf message1.tar.xz [root@localhost test]# ll -h 總用量 896K -rw-r--r--. 1 root root 890K 8月 19 17:16 message1.tar.xz drwxr-xr-x. 2 root root 4.0K 8月 19 17:14 test

這裡使用tar -xf為什麼不加前綴J?因為tar在展開歸檔時會自動檢測指定要展開的壓縮包,命令會根據後綴自動選擇相應的工具進行解壓。


本文總結

1、文件的後綴原本對Linux系統無實際意義,但在壓縮解壓是個例外

2、gzip、bzip2、xz都只支持壓縮文件無法壓縮目錄,若要對目錄進行壓縮則需先進行歸檔之後對歸檔的文件進行壓縮。

3、tar只能對文件歸檔不能壓縮解壓操作不過它可以調用gzip、bzip2、xz實現歸檔壓縮及解壓並展開歸檔。

4、tar僅僅做歸檔操作,其產生的tar包大於等於原來的文件夾大小。

5、tar展開壓縮歸檔文件無需事先指明要解壓擴展的文件的壓縮工具,因為其可以自動識別。

6、壓縮及解壓縮是以犧牲CPU時間為代價節省磁盤空間及傳輸文件時間,若是壓縮解壓10MB以上的文件,可以明顯感覺到命令執行時間變長。

Copyright © Linux教程網 All Rights Reserved