天天看點

Linux 指令(檔案和目錄管理 - gzip/gunzip)

簡述

和 zip 指令類似,gzip 用于檔案的壓縮,gzip 壓縮後的檔案擴充名位 .gz,gzip 預設壓縮後會删除原檔案。gunzip 用于解壓經過 gzip 壓縮過的檔案。

| 版權聲明:一去、二三裡,未經部落客允許不得轉載。

gzip

指令介紹

  • 指令名稱

    gzip

  • 基本文法

    gzip [OPTION]… [FILE]…

  • 功能描述

    壓縮檔案。壓縮後的檔案擴充名位 .gz,預設壓縮後會删除原檔案。

指令選項

選項 說明

​-a --ascii​

使用 ASCII 文字模式

​-c, --stdout​

把壓縮後的檔案輸出到标準輸出裝置,保持原始檔案不變。

​-d, --decompress​

對壓縮的檔案進行解壓

​-f, --force​

強行壓縮檔案,不理會檔案名稱或硬連接配接是否存在以及該檔案是否為符号連接配接。

​-h, --help​

線上幫助

​-l, --list​

列出壓縮檔案的相關資訊

​-L, --license​

顯示 license

​-n, --no-name​

壓縮檔案時,不儲存或恢複原來的名字和時間戳

​-N, --name​

壓縮檔案時,儲存或恢複原來的名字和時間戳

​-q, --quiet​

不顯示警告資訊

​-r, --recursive​

遞歸壓縮指定目錄以及子目錄下的所有檔案

​-S, --suffix=SU​

更改壓縮檔案字尾字元串為 SUF

​-t, --test​

檢查壓縮文檔的完整性

​-v, --verbose​

顯示指令執行過程

​-V, --version​

顯示版本資訊

​-# --fast --best​

用指定的數字 # 調整壓縮速度,-1 或 ​

​--fast​

​​ 表示最快壓縮方法(低壓縮比),-9 或 ​

​--best​

​ 表示最慢壓縮方法(高壓縮比)。系統預設值為6(偏高壓縮比)。

使用範例

1.壓縮檔案,壓縮後原檔案被删除

[wang@localhost linux]$ ls -l
總用量 33132
-rw-rw-r--. 1 wang wang       31 9月  29 11:03 hello.sh
-rw-r--r--. 1 wang wang 33921784 6月   7 11:02 linux-program.pdf
# 壓縮檔案 hello.sh,壓縮後的檔案為 hello.sh.gz
[wang@localhost linux]$ gzip hello.sh 
[wang@localhost linux]$ ls -l
總用量 33132
-rw-rw-r--. 1 wang wang       60 9月  29 11:03 hello.sh.gz
-rw-r--r--. 1 wang wang 33921784 6月   7 11:02 linux-program.pdf
# gzip 壓縮過的檔案的特性
[wang@localhost linux]$ file hello.sh.gz 
hello.sh.gz: gzip compressed data, was "hello.sh", from Unix, last modified: Thu Sep 29 11:03:24 2016      

2.壓縮後,保留原檔案

# 計算壓縮後檔案的 md5 值
[wang@localhost linux]$ gzip hello.sh 
[wang@localhost linux]$ md5sum hello.sh.gz 
edf22795963c6264df42970855a14efe  hello.sh.gz
# 解壓縮(還原回去)
[wang@localhost linux]$ gunzip hello.sh.gz
# 如果要保留原檔案,使用下面指令
[wang@localhost linux]$ gzip -c hello.sh >hello.sh.gz
[wang@localhost linux]$ ls
hello.sh  hello.sh.gz  linux-program.pdf
[wang@localhost linux]$ md5sum hello.sh.gz 
edf22795963c6264df42970855a14efe  hello.sh.gz      

顯然,校驗 md5 值,和直接使用 gzip 一緻。

3.壓縮時,顯示指令執行過程

[wang@localhost linux]$ gzip -v hello.sh 
hello.sh:    -6.5% -- replaced with hello.sh.gz
[wang@localhost linux]$ ls -l
總用量 33132
-rw-rw-r--. 1 wang wang       60 9月  29 11:03 hello.sh.gz
-rw-r--r--. 1 wang wang 33921784 6月   7 11:02 linux-program.pdf      

4.将目錄 c 下的每個檔案壓縮成 .gz 檔案

[wang@localhost c]$ ls -l
總用量 16976
-rw-rw-r--. 1 wang wang      60 9月  29 11:05-rw-r--r--. 1 wang wang 7283461 12月 14 2015 QmlBook-In-Chinese.pdf
-rw-r--r--. 1 wang wang 7484327 12月 16 2015-rw-r--r--. 1 wang wang 2604948 1月  18 2016 qwt-6.1.1.pdf
[wang@localhost c]$ gzip *
[wang@localhost c]$ ls -l
總用量 15052
-rw-rw-r--. 1 wang wang      86 9月  29 11:05-rw-r--r--. 1 wang wang 5778415 12月 14 2015 QmlBook-In-Chinese.pdf.gz
-rw-r--r--. 1 wang wang 7157026 12月 16 2015-rw-r--r--. 1 wang wang 2466302 1月  18 2016 qwt-6.1.1.pdf.gz      

注意:如果是目錄,将被忽略。

5.将每個被壓縮的檔案解壓

[wang@localhost c]$ ls -l
總用量 15052
-rw-rw-r--. 1 wang wang      86 9月  29 11:05-rw-r--r--. 1 wang wang 5778415 12月 14 2015 QmlBook-In-Chinese.pdf.gz
-rw-r--r--. 1 wang wang 7157026 12月 16 2015-rw-r--r--. 1 wang wang 2466302 1月  18 2016 qwt-6.1.1.pdf.gz
[wang@localhost c]$ gzip -d *
[wang@localhost c]$ ls -l
總用量 16976
-rw-rw-r--. 1 wang wang      60 9月  29 11:05-rw-r--r--. 1 wang wang 7283461 12月 14 2015 QmlBook-In-Chinese.pdf
-rw-r--r--. 1 wang wang 7484327 12月 16 2015-rw-r--r--. 1 wang wang 2604948 1月  18 2016 qwt-6.1.1.pdf      

6.詳細顯壓縮檔案的資訊(不解壓)

[wang@localhost c]$ gzip -l *
         compressed        uncompressed  ratio uncompressed_name
                 86                  60   0.0% hello.c
            5778415             7283461  20.7% QmlBook-In-Chinese.pdf
            7157026             7484327   4.4% qt5_cadaques.pdf
            2466302             2604948   5.3% qwt-6.1.1.pdf
           15401829            17372796  11.3% (totals)      

7.遞歸的壓縮目錄

使用 -r 選項,遞歸壓縮 doc 目錄以及子目錄下的所有檔案(目錄依然存在)。

# 壓縮前的目錄樹
[wang@localhost ~]$ tree doc
doc
├── c
│   ├── hello.c
│   ├── QmlBook-In-Chinese.pdf
│   ├── qt5_cadaques.pdf
│   └── qwt-6.1.1.pdf
├── css
├── debug.log
├── js
├── linux
│   ├── hello.sh
│   └── linux-program.pdf
└── program

4 directories, 8 files
# 遞歸壓縮 doc 目錄
[wang@localhost ~]$ gzip -rv doc
doc/linux/linux-program.pdf:      2.1% -- replaced with doc/linux/linux-program.pdf.gz
doc/linux/hello.sh:  -6.5% -- replaced with doc/linux/hello.sh.gz
doc/c/hello.c:    0.0% -- replaced with doc/c/hello.c.gz
doc/c/QmlBook-In-Chinese.pdf:    20.7% -- replaced with doc/c/QmlBook-In-Chinese.pdf.gz
doc/c/qt5_cadaques.pdf:   4.4% -- replaced with doc/c/qt5_cadaques.pdf.gz
doc/c/qwt-6.1.1.pdf:      5.3% -- replaced with doc/c/qwt-6.1.1.pdf.gz
doc/debug.log:   97.8% -- replaced with doc/debug.log.gz
doc/program:      0.0% -- replaced with doc/program.gz
# 壓縮後的目錄樹
[wang@localhost ~]$ tree doc
doc
├── c
│   ├── hello.c.gz
│   ├── QmlBook-In-Chinese.pdf.gz
│   ├── qt5_cadaques.pdf.gz
│   └── qwt-6.1.1.pdf.gz
├── css
├── debug.log.gz
├── js
├── linux
│   ├── hello.sh.gz
│   └── linux-program.pdf.gz
└── program.gz

4 directories, 8      

8.遞歸的解壓目錄

# 解壓前的目錄樹
[wang@localhost ~]$ tree doc
doc
├── c
│   ├── hello.c.gz
│   ├── QmlBook-In-Chinese.pdf.gz
│   ├── qt5_cadaques.pdf.gz
│   └── qwt-6.1.1.pdf.gz
├── css
├── debug.log.gz
├── js
├── linux
│   ├── hello.sh.gz
│   └── linux-program.pdf.gz
└── program.gz

4 directories, 8 files
# 遞歸解壓 doc 目錄
[wang@localhost ~]$ gzip -dr doc
# 解壓後的目錄樹
[wang@localhost ~]$ tree doc
doc
├── c
│   ├── hello.c
│   ├── QmlBook-In-Chinese.pdf
│   ├── qt5_cadaques.pdf
│   └── qwt-6.1.1.pdf
├── css
├── debug.log
├── js
├── linux
│   ├── hello.sh
│   └── linux-program.pdf
└── program

4 directories, 8      

gunzip

指令介紹

  • 指令名稱

    gunzip

  • 基本文法

    gunzip [OPTION]… [FILE]…

  • 功能描述

    解壓縮檔案

指令選項

選項 說明

​-a --ascii​

使用 ASCII 文字模式

​-c, --stdout​

把壓縮後的檔案輸出到标準輸出裝置,保持原始檔案不變。

​-f, --force​

強行解壓縮檔案,不理會檔案名稱或硬連接配接是否存在以及該檔案是否為符号連接配接。

​-l, --list​

列出壓縮檔案的相關資訊

​-n, --no-name​

解壓縮檔案時,不儲存或恢複原來的名字和時間戳

​-N, --name​

解壓縮檔案時,儲存或恢複原來的名字和時間戳

​-q, --quiet​

不顯示警告資訊

​-r, --recursive​

遞歸解壓縮指定目錄以及子目錄下的所有檔案

​-S, --suffix=SU​

更改壓縮檔案字尾字元串為 SUF

​-t, --test​

檢查壓縮文檔的完整性

​-v, --verbose​

顯示指令執行過程

​--help​

顯示幫助資訊并退出

​--version​

顯示版本資訊并退出

使用範例

1.解壓縮檔案

[wang@localhost linux]$ ls
hello.sh.gz  linux-program.pdf
[wang@localhost linux]$ gunzip hello.sh.gz 
[wang@localhost linux]$ ls
hello.sh  linux-program.pdf      

2.詳細顯示壓縮檔案的資訊(不解壓)

[wang@localhost linux]$ gunzip -l hello.sh.gz 
         compressed        uncompressed  ratio uncompressed_name
                 60                  31  -6.5% hello.sh      

3.解壓縮時,顯示指令執行過程

[wang@localhost linux]$ gunzip -v hello.sh.gz 
hello.sh.gz:     -6.5% -- replaced with hello.sh
[wang@localhost linux]$ ls
hello.sh  linux-program.pdf      

4.​

​gzip –d​

​ 等價于 gunzip

# 原檔案
[wang@localhost linux]$ ls
hello.sh  linux-program.pdf
[wang@localhost linux]$ md5sum hello.sh 
97a6ab3ff888b1ff6519df2387983aa5  hello.sh
# gzip 壓縮後,根據 gzip -d 解壓縮
[wang@localhost linux]$ gzip hello.sh 
[wang@localhost linux]$ ls
hello.sh.gz  linux-program.pdf
[wang@localhost linux]$ gzip -d hello.sh.gz 
[wang@localhost linux]$ ls
hello.sh  linux-program.pdf
[wang@localhost linux]$ md5sum hello.sh 
97a6ab3ff888b1ff6519df2387983aa5  hello.sh
# gzip 壓縮後,根據 gunzip 解壓縮
[wang@localhost linux]$ gzip hello.sh 
[wang@localhost linux]$ ls
hello.sh.gz  linux-program.pdf
[wang@localhost linux]$ gunzip hello.sh.gz 
[wang@localhost linux]$ ls
hello.sh  linux-program.pdf
[wang@localhost linux]$ md5sum hello.sh 
97a6ab3ff888b1ff6519df2387983aa5  hello.sh      

繼續閱讀