再強調一下,gzip 命令只能用來壓縮文件,不能壓縮目錄,即便指定了目錄,也只能壓縮目錄內的所有文件。
gzip 命令的基本格式如下:[root@localhost ~]# gzip [選項] 源文件
命令中的源文件,當進行壓縮操作時,指的是普通文件;當進行解壓縮操作時,指的是壓縮文件。該命令常用的選項及含義如表 1 所示。| 選項 | 含義 |
|---|---|
| -c | 將壓縮數據輸出到標準輸出中,并保留源文件。 |
| -d | 對壓縮文件進行解壓縮。 |
| -r | 遞歸壓縮指定目錄下以及子目錄下的所有文件。 |
| -v | 對于每個壓縮和解壓縮的文件,顯示相應的文件名和壓縮比。 |
| -l | 對每一個壓縮文件,顯示以下字段:
|
| -數字 | 用于指定壓縮等級,-1 壓縮等級最低,壓縮比最差;-9 壓縮比最高。默認壓縮比是 -6。 |
[root@localhost ~]# gzip install.log
#壓縮instal.log 文件
[root@localhost ~]# ls
anaconda-ks.cfg install.log.gz install.log.syslog
#壓縮文件生成,但是源文件也消失了
[root@localhost ~]# gzip -c anaconda-ks.cfg >anaconda-ks.cfg.gz
#使用-c選項,但是不讓壓縮數據輸出到屏幕上,而是重定向到壓縮文件中,這樣可以縮文件的同時不刪除源文件
[root@localhost ~]# ls
anaconda-ks.cfg anaconda-ks.cfg.gz install.log.gz install.log.syslog
#可以看到壓縮文件和源文件都存在
[root@localhost ~]# mkdir test
[root@localhost ~]# touch test/test1
[root@localhost ~]# touch test/test2
[root@localhost ~]# touch test/test3 #建立測試目錄,并在里面建立幾個測試文件
[root@localhost ~]# gzip -r test/
#壓縮目錄,并沒有報錯
[root@localhost ~]# ls
anaconda-ks.cfg anaconda-ks.cfg.gz install.log.gz install.log.syslog test
#但是查看發現test目錄依然存在,并沒有變為壓縮文件
[root@localhost ~]# ls test/
testl .gz test2.gz test3.gz
#原來gzip命令不會打包目錄,而是把目錄下所有的子文件分別壓縮
新聞熱點
疑難解答