減少檔案大小有兩個明顯的好處,一是可以減少存儲空間,二是通過網絡傳輸檔案時,可以減少傳輸的時間。gzip是在linux系統中經常使用的一個對檔案進行壓縮和解壓縮的指令,既友善又好用。gzip不僅可以用來壓縮大的、較少使用的檔案以節省磁盤空間,還可以和tar指令一起構成linux作業系統中比較流行的壓縮檔案格式。據統計,gzip指令對文本檔案有60%~70%的壓縮率。
1.指令格式:
gzip[參數][檔案或者目錄]
2.指令功能:
gzip是個使用廣泛的壓縮程式,檔案經它壓縮過後,其名稱後面會多出".gz"的擴充名。
3.指令參數:
-a或--ascii 使用ascii文字模式。
-c或--stdout或--to-stdout 把壓縮後的檔案輸出到标準輸出裝置,不去更動原始檔案。
-d或--decompress或----uncompress 解開壓縮檔案。
-f或--force 強行壓縮檔案。不理會檔案名稱或硬連接配接是否存在以及該檔案是否為符号連接配接。
-h或--help 線上幫助。
-l或--list 列出壓縮檔案的相關資訊。
-l或--license 顯示版本與版權資訊。
-n或--no-name 壓縮檔案時,不儲存原來的檔案名稱及時間戳記。
-n或--name 壓縮檔案時,儲存原來的檔案名稱及時間戳記。
-q或--quiet 不顯示警告資訊。
-r或--recursive 遞歸處理,将指定目錄下的所有檔案及子目錄一并處理。
-s<壓縮字尾字元串>或----suffix<壓縮字尾字元串> 更改壓縮字尾字元串。
-t或--test 測試壓縮檔案是否正确無誤。
-v或--verbose 顯示指令執行過程。
-v或--version 顯示版本資訊。
-num 用指定的數字num調整壓縮的速度,-1或--fast表示最快壓縮方法(低壓縮比),-9或--best表示最慢壓縮方法(高壓縮比)。系統預設值為6。
4.使用執行個體:
執行個體1:把test6目錄下的每個檔案壓縮成.gz檔案
指令:
gzip *
輸出:
[root@localhost test6]# ll
總計 604
---xr--r-- 1 root mail 302108 11-30 08:39 linklog.log
---xr--r-- 1 mail users 302108 11-30 08:39 log2012.log
-rw-r--r-- 1 mail users 61 11-30 08:39 log2013.log
-rw-r--r-- 1 root mail 0 11-30 08:39 log2014.log
-rw-r--r-- 1 root mail 0 11-30 08:39 log2015.log
-rw-r--r-- 1 root mail 0 11-30 08:39 log2016.log
-rw-r--r-- 1 root mail 0 11-30 08:39 log2017.log
[root@localhost test6]# gzip *
總計 28
---xr--r-- 1 root mail 1341 11-30 08:39 linklog.log.gz
---xr--r-- 1 mail users 1341 11-30 08:39 log2012.log.gz
-rw-r--r-- 1 mail users 70 11-30 08:39 log2013.log.gz
-rw-r--r-- 1 root mail 32 11-30 08:39 log2014.log.gz
-rw-r--r-- 1 root mail 32 11-30 08:39 log2015.log.gz
-rw-r--r-- 1 root mail 32 11-30 08:39 log2016.log.gz
-rw-r--r-- 1 root mail 32 11-30 08:39 log2017.log.gz
[root@localhost test6]#
說明:
執行個體2:把例1中每個壓縮的檔案解壓,并列出詳細的資訊
gzip -dv *
[root@localhost test6]# gzip -dv *
linklog.log.gz: 99.6% -- replaced with linklog.log
log2012.log.gz: 99.6% -- replaced with log2012.log
log2013.log.gz: 47.5% -- replaced with log2013.log
log2014.log.gz: 0.0% -- replaced with log2014.log
log2015.log.gz: 0.0% -- replaced with log2015.log
log2016.log.gz: 0.0% -- replaced with log2016.log
log2017.log.gz: 0.0% -- replaced with log2017.log
執行個體3:詳細顯示例1中每個壓縮的檔案的資訊,并不解壓
gzip -l *
[root@localhost test6]# gzip -l *
compressed uncompressed ratio uncompressed_name
1341 302108 99.6% linklog.log
1341 302108 99.6% log2012.log
70 61 47.5% log2013.log
32 0 0.0% log2014.log
32 0 0.0% log2015.log
32 0 0.0% log2016.log
32 0 0.0% log2017.log
2880 604277 99.5% (totals)
執行個體4:壓縮一個tar備份檔案,此時壓縮檔案的擴充名為.tar.gz
gzip -r log.tar
[root@localhost test]# ls -al log.tar
-rw-r--r-- 1 root root 307200 11-29 17:54 log.tar
[root@localhost test]# gzip -r log.tar
[root@localhost test]# ls -al log.tar.gz
-rw-r--r-- 1 root root 1421 11-29 17:54 log.tar.gz
執行個體5:遞歸的壓縮目錄
gzip -rv test6
[root@localhost test6]# cd ..
[root@localhost test]# gzip -rv test6
test6/log2014.log: 0.0% -- replaced with test6/log2014.log.gz
test6/linklog.log: 99.6% -- replaced with test6/linklog.log.gz
test6/log2015.log: 0.0% -- replaced with test6/log2015.log.gz
test6/log2013.log: 47.5% -- replaced with test6/log2013.log.gz
test6/log2012.log: 99.6% -- replaced with test6/log2012.log.gz
test6/log2017.log: 0.0% -- replaced with test6/log2017.log.gz
test6/log2016.log: 0.0% -- replaced with test6/log2016.log.gz
[root@localhost test]# cd test6
這樣,所有test下面的檔案都變成了*.gz,目錄依然存在隻是目錄裡面的檔案相應變成了*.gz.這就是壓縮,和打包不同。因為是對目錄操作,是以需要加上-r選項,這樣也可以對子目錄進行遞歸了。
執行個體6:遞歸地解壓目錄
gzip -dr test6
[root@localhost test]# gzip -dr test6