linux每日命令 32 gzip命令

2021-09-08 20:26:37 字數 4882 閱讀 5331

減少檔案大小有兩個明顯的好處,一是可以減少儲存空間,二是通過網路傳輸檔案時,可以減少傳輸的時間。gzip是在linux系統中經常使用的乙個對檔案進行壓縮和解壓縮的命令,既方便又好用。gzip不僅可以用來壓縮大的、較少使用的檔案以節省磁碟空間,還可以和tar命令一起構成linux作業系統中比較流行的壓縮檔案格式。據統計,gzip命令對文字檔案有60%~70%的壓縮率。

gzip [引數] [檔案或者目錄]
gzip是個使用廣泛的壓縮程式,檔案經它壓縮過後,其名稱後面會多出".gz"的副檔名

引數描述

-a或--ascii , 使用ascii文字模式。

-c或--stdout或--to-stdout ,把壓縮後的檔案輸出到標準輸出裝置,不去變動原始檔案。

-d或--decompress或----uncompress,解開壓縮檔案

-r或--recursive , 遞迴處理,將指定目錄下的所有檔案及子目錄一併處理。

-f或--force , 強行壓縮檔案。不理會檔名稱或硬連線是否存在以及該檔案是否為符號連線。

-l或--list,  列出壓縮檔案的相關資訊。

-n或--no-name  ,壓縮檔案時,不儲存原來的檔名稱及時間戳記。

-n或--name  ,壓縮檔案時,儲存原來的檔名稱及時間戳記。

-q或--quiet ,  不顯示警告資訊。

-s或----suffix  ,更改壓縮字尾字串。

-t或--test , 測試壓縮檔案是否正確無誤。

-v或--verbose  ,顯示指令執行過程

-v或--version , 顯示版本資訊。

-l或--license  顯示版本與版權資訊。

-num

用指定的數字num調整壓縮的速度,-1或--fast表示最快壓縮方法(低壓縮比),-9或--best表示最慢壓縮方法(高壓縮比)。系統預設值為6。

-h命令:

gzip *
輸出:

[root@localhost test]# ll

total 12

-rw-r--r-- 1 root root 2117 12月 1 09:08 1.log

-rw-r--r-- 1 root root 2199 12月 1 09:08 2.log

-rw-r--r-- 1 root root 2117 12月 1 09:08 3.log

[root@localhost test]# gzip *

[root@localhost test]# ll

total 12

-rw-r--r-- 1 root root 1189 12月 1 09:08 1.log.gz

-rw-r--r-- 1 root root 1245 12月 1 09:08 2.log.gz

-rw-r--r-- 1 root root 1189 12月 1 09:08 3.log.gz

說明:

將test目錄下的每個檔案壓縮成.gz檔案

命令:

gzip -dv *
輸出:

[root@localhost test]#  touch 4.log

[root@localhost test]# ll

total 12

-rw-r--r-- 1 root root 1189 12月 1 09:08 1.log.gz

-rw-r--r-- 1 root root 1245 12月 1 09:08 2.log.gz

-rw-r--r-- 1 root root 1189 12月 1 09:08 3.log.gz

-rw-r--r-- 1 root root 0 12月 1 09:17 4.log

[root@localhost test]# gzip -dv *

1.log.gz: 45.0% -- replaced with 1.log

2.log.gz: 44.5% -- replaced with 2.log

3.log.gz: 45.0% -- replaced with 3.log

gzip: 4.log: unknown suffix -- ignored

[root@localhost test]# ll

total 12

-rw-r--r-- 1 root root 2117 12月 1 09:08 1.log

-rw-r--r-- 1 root root 2199 12月 1 09:08 2.log

-rw-r--r-- 1 root root 2117 12月 1 09:08 3.log

-rw-r--r-- 1 root root 0 12月 1 09:17 4.log

說明:

將test目錄下的每個已壓縮的檔案進行解壓

命令:

gzip -l *
輸出:

[root@localhost test]# ll

total 12

-rw-r--r-- 1 root root 1189 12月 1 09:08 1.log.gz

-rw-r--r-- 1 root root 1245 12月 1 09:08 2.log.gz

-rw-r--r-- 1 root root 1189 12月 1 09:08 3.log.gz

[root@localhost test]# gzip -l *

compressed uncompressed ratio uncompressed_name

1189 2117 45.0% 1.log

1245 2199 44.5% 2.log

1189 2117 45.0% 3.log

3623 6433 44.1% (totals)

說明:

詳細顯示例1中的每個壓縮檔案的資訊,但不進行解壓

命令:

gzip -rv test
輸出:

[root@localhost test]# ll

total 12

-rw-r--r-- 1 root root 2117 12月 1 09:08 1.log

-rw-r--r-- 1 root root 2199 12月 1 09:08 2.log

-rw-r--r-- 1 root root 2117 12月 1 09:08 3.log

[root@localhost test]# cd ..

[root@localhost hc]# gzip -v test

gzip: test is a directory -- ignored

[root@localhost hc]# gzip -rv test

test/1.log: 45.0% -- replaced with test/1.log.gz

test/2.log: 44.5% -- replaced with test/2.log.gz

test/3.log: 45.0% -- replaced with test/3.log.gz

[root@localhost hc]# cd test

[root@localhost test]# ll

total 12

-rw-r--r-- 1 root root 1189 12月 1 09:08 1.log.gz

-rw-r--r-- 1 root root 1245 12月 1 09:08 2.log.gz

-rw-r--r-- 1 root root 1189 12月 1 09:08 3.log.gz

說明:

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

如果要壓縮成乙個gz檔案,可以先用tar命令對目錄進行打包,然後再對打包檔案使用gzip命令

命令:

gzip -drv   test
輸出:

[root@localhost test]# ll

total 12

-rw-r--r-- 1 root root 1189 12月 1 09:08 1.log.gz

-rw-r--r-- 1 root root 1245 12月 1 09:08 2.log.gz

-rw-r--r-- 1 root root 1189 12月 1 09:08 3.log.gz

[root@localhost test]# cd ..

[root@localhost hc]# gzip -drv test

test/1.log.gz: 45.0% -- replaced with test/1.log

test/2.log.gz: 44.5% -- replaced with test/2.log

test/3.log.gz: 45.0% -- replaced with test/3.log

[root@localhost hc]# cd test

[root@localhost test]# ll

total 12

-rw-r--r-- 1 root root 2117 12月 1 09:08 1.log

-rw-r--r-- 1 root root 2199 12月 1 09:08 2.log

-rw-r--r-- 1 root root 2117 12月 1 09:08 3.log

linux基本命令(32) gzip命令

減少檔案大小有兩個明顯的好處,一是可以減少儲存空間,二是通過網路傳輸檔案時,可以減少傳輸的時間。gzip是在linux系統中經常使用的乙個對檔案進行壓縮和解壓縮的命令,既方便又好用。gzip不僅可以用來壓縮大的 較少使用的檔案以節省磁碟空間,還可以和tar命令一起構成linux作業系統中比較流行的壓...

每天乙個linux命令(32) gzip命令

gzip命令對文字檔案有60 70 的壓縮率。gzip 引數 檔案或者目錄 gzip是個使用廣泛的壓縮程式,檔案經它壓縮過後,其名稱後面會多出 gz 的副檔名。a或 ascii 使用ascii文字模式。c或 stdout或 to stdout 把壓縮後的檔案輸出到標準輸出裝置,不去更動原始檔案。d或...

linux命令 gzip壓縮

壓縮命令gzip root wangshaojun ls 111.txt 222install.log.syslog root wangshaojun cat etc init.d iptables install.log.syslog root wangshaojun cat etc init.d...