Linux常用命令(二十六)-gzip

來源:互聯網
上載者:User

Linux常用命令(二十六)-gzip

減少檔案大小有兩個明顯的好處,一是可以減少儲存空間,二是通過網路傳輸檔案時,可以減少傳輸的時間。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命令執行個體:

執行個體一. 把test6目錄下的每個檔案壓縮成.gz檔案

命令:gzip *

 

[root@localhost test6]# ll---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[root@localhost test6]# gzip *[root@localhost test6]# ll---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[root@localhost test6]#

 

 

執行個體二. 把例1中每個壓縮的檔案解壓,並列出詳細的資訊

命令:gzip -dv *

 

[root@localhost test6]# ll總計 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[root@localhost test6]# gzip -dv *linklog.log.gz:  99.6% -- replaced with linklog.loglog2012.log.gz:  99.6% -- replaced with log2012.loglog2013.log.gz:  47.5% -- replaced with log2013.log[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[root@localhost test6]#

執行個體三. 詳細顯樣本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)

執行個體四. 壓縮一個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

 

 

執行個體五. 遞迴地壓縮目錄

命令:gzip -rv test6
說明:所有test下面的檔案都變成了*.gz,目錄依然存在只是目錄裡面的檔案相應變成了*.gz.這就是壓縮,和打包不同。因為是對目錄操作,所以需要加上-r選項,這樣也可以對子目錄進行遞迴了。

 

[root@localhost test6]# ll---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[root@localhost test6]# cd ..[root@localhost test]# gzip -rv test6test6/linklog.log:       99.6% -- replaced with test6/linklog.log.gztest6/log2013.log:       47.5% -- replaced with test6/log2013.log.gztest6/log2012.log:       99.6% -- replaced with test6/log2012.log.gz[root@localhost test]# cd test6[root@localhost test6]# ll---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

 

 

執行個體六. 遞迴地解縮目錄
命令:gzip -dr test6

 

[root@localhost test6]# ll---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[root@localhost test6]# cd ..[root@localhost test]# gzip -dr test6[root@localhost test]# cd test6[root@localhost test6]# ll---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[root@localhost test6]#

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.