簡明Linux命令行筆記:gzip


壓縮和解壓縮文件

gzip [options] [file-list]

gunzip [options] [file-list]

zcat [file-list]

 

gzip程序用來壓縮文件,gunzip程序用來還原gzip壓縮的文件,zcat程序用來顯示gzip壓縮的文件

壓縮后會刪除原始文件,壓縮過的文件后綴為.gz

 

參數

file-list 為要壓縮或解壓縮的一個或者多個文件的路徑名列表。如果在file-list中存在目錄但沒有選項-r,則gzip/gunzip會報告錯誤消息並忽略該目錄

使用-r選項,gzip/gunzip會遞歸地壓縮/解壓縮目錄層次結構中的文件

 

選項

-c                 將壓縮或解壓縮的結果寫到標准輸出,而不是寫到文件

-d                 解壓縮用gzip壓縮的文件。該選項用於gzip,與gunzip等價

-f                  壓縮/解壓縮時強制覆蓋已存在的文件

-l                  對於在file-list中要壓縮的每個文件,顯示壓縮和解壓縮后文件的大小、壓縮比和壓縮前的文件名

                    -v可獲得其他信息

-n                 在壓縮速度和壓縮量間進行權衡。n為從1~9的數字。第1級為最快壓縮,但壓縮量最小;第9級的壓縮速度最慢,但壓縮量最大。

                    默認級別為6,--fast和--best分別等價於-1和-9

-q                 禁止顯示警告信息

-r                  遞歸壓縮/解壓縮file-list中的文件

-t                  驗證壓縮文件的完整性。如果文件完整則不顯示任何信息

-v                 顯示文件名、壓縮后的文件名和每個被處理文件的壓縮量

 

示例

gzip

siu@Darling ~/work $ ls
a  b  c  dir
siu@Darling ~/work $ gzip a
siu@Darling ~/work $ ls
a.gz  b  c  dir
siu@Darling ~/work $ 

壓縮文件,並刪除原始文件

 

gzip -v

siu@Darling ~/work $ gzip -v b
b:     26.4% -- replaced with b.gz
siu@Darling ~/work $ ls
a.gz  b.gz  c  dir

壓縮文件,並顯示壓縮信息

 

gunzip

siu@Darling ~/work $ gunzip a.gz 
siu@Darling ~/work $ ls
a  b.gz  c  dir

解壓文件,gunzip == gzip -d

 

zcat

siu@Darling ~/work $ zcat b.gz 
Picture perfect memories scattered all around the floor
Reaching for the phone 'cause I can't fight it anymore
And I wonder if I ever cross your mind
For me it happens all the time

不解壓而顯示壓縮文件的內容

 

gunzip -c

siu@Darling ~/work $ gunzip -c b.gz 
Picture perfect memories scattered all around the floor
Reaching for the phone 'cause I can't fight it anymore
And I wonder if I ever cross your mind
For me it happens all the time

解壓文件到標准輸出,也就是一解壓就輸出內容到屏幕,不改動文件

 

gunzip -l

siu@Darling ~/work $ gunzip -l b.gz 
         compressed        uncompressed  ratio uncompressed_name
                154                 182  26.4% b
siu@Darling ~/work $ ls
a  b.gz  c  dir

顯示壓縮和解壓縮的信息,但並不執行解壓縮

 

gzip -9 -r

siu@Darling ~/work $ gzip -9 -r dir
siu@Darling ~/work $ ls
a  b.gz  c  dir
siu@Darling ~/work $ ls -l dir
總用量 12
-rw-r--r-- 1 siu siu 154  1月 12 16:22 a.gz
-rw-r--r-- 1 siu siu 154  1月 12 16:22 b.gz
-rw-r--r-- 1 siu siu 154  1月 12 16:22 c.gz

用壓縮量最大的方式遞歸壓縮目錄中的文件

 

Tips

gzip對文本文件壓縮效果較明顯,二進制文件或大文件推薦使用bzip2壓縮

                    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM