1. 程式人生 > >Linux 命令(檔案和目錄管理

Linux 命令(檔案和目錄管理

簡述

和 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.壓縮檔案,壓縮後原檔案被刪除

[[email protected] linux]$ ls -l
總用量 33132
-rw-rw-r--. 1
wang wang 31 929 11:03 hello.sh -rw-r--r--. 1 wang wang 33921784 67 11:02 linux-program.pdf # 壓縮檔案 hello.sh,壓縮後的檔案為 hello.sh.gz [[email protected] linux]$ gzip hello.sh [[email protected] linux]$ ls -l 總用量 33132 -rw-rw-r--. 1 wang wang 60 929 11:03 hello.sh.gz -rw-r--r--. 1 wang wang 33921784 67 11:02 linux-program.pdf # gzip 壓縮過的檔案的特性 [[email protected] 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 值
[[email protected] linux]$ gzip hello.sh 
[[email protected] linux]$ md5sum hello.sh.gz 
edf22795963c6264df42970855a14efe  hello.sh.gz
# 解壓縮(還原回去)
[[email protected] linux]$ gunzip hello.sh.gz
# 如果要保留原檔案,使用下面命令
[[email protected] linux]$ gzip -c hello.sh >hello.sh.gz
[[email protected] linux]$ ls
hello.sh  hello.sh.gz  linux-program.pdf
[[email protected] linux]$ md5sum hello.sh.gz 
edf22795963c6264df42970855a14efe  hello.sh.gz

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

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

[[email protected] linux]$ gzip -v hello.sh 
hello.sh:    -6.5% -- replaced with hello.sh.gz
[[email protected] linux]$ ls -l
總用量 33132
-rw-rw-r--. 1 wang wang       60 929 11:03 hello.sh.gz
-rw-r--r--. 1 wang wang 33921784 67 11:02 linux-program.pdf

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

[[email protected] c]$ ls -l
總用量 16976
-rw-rw-r--. 1 wang wang      60 929 11:05 hello.c
-rw-r--r--. 1 wang wang 7283461 1214 2015 QmlBook-In-Chinese.pdf
-rw-r--r--. 1 wang wang 7484327 1216 2015 qt5_cadaques.pdf
-rw-r--r--. 1 wang wang 2604948 118 2016 qwt-6.1.1.pdf
[[email protected] c]$ gzip *
[[email protected] c]$ ls -l
總用量 15052
-rw-rw-r--. 1 wang wang      86 929 11:05 hello.c.gz
-rw-r--r--. 1 wang wang 5778415 1214 2015 QmlBook-In-Chinese.pdf.gz
-rw-r--r--. 1 wang wang 7157026 1216 2015 qt5_cadaques.pdf.gz
-rw-r--r--. 1 wang wang 2466302 118 2016 qwt-6.1.1.pdf.gz

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

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

[[email protected] c]$ ls -l
總用量 15052
-rw-rw-r--. 1 wang wang      86 929 11:05 hello.c.gz
-rw-r--r--. 1 wang wang 5778415 1214 2015 QmlBook-In-Chinese.pdf.gz
-rw-r--r--. 1 wang wang 7157026 1216 2015 qt5_cadaques.pdf.gz
-rw-r--r--. 1 wang wang 2466302 118 2016 qwt-6.1.1.pdf.gz
[[email protected] c]$ gzip -d *
[[email protected] c]$ ls -l
總用量 16976
-rw-rw-r--. 1 wang wang      60 929 11:05 hello.c
-rw-r--r--. 1 wang wang 7283461 1214 2015 QmlBook-In-Chinese.pdf
-rw-r--r--. 1 wang wang 7484327 1216 2015 qt5_cadaques.pdf
-rw-r--r--. 1 wang wang 2604948 118 2016 qwt-6.1.1.pdf

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

[[email protected] 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 目錄以及子目錄下的所有檔案(目錄依然存在)。

# 壓縮前的目錄樹
[[email protected] ~]$ 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 目錄
[[email protected] ~]$ 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
# 壓縮後的目錄樹
[[email protected] ~]$ 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

8.遞迴的解壓目錄

# 解壓前的目錄樹
[[email protected] ~]$ 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 目錄
[[email protected] ~]$ gzip -dr doc
# 解壓後的目錄樹
[[email protected] ~]$ 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

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.解壓縮檔案

[[email protected] linux]$ ls
hello.sh.gz  linux-program.pdf
[[email protected] linux]$ gunzip hello.sh.gz 
[[email protected] linux]$ ls
hello.sh  linux-program.pdf

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

[[email protected] linux]$ gunzip -l hello.sh.gz 
         compressed        uncompressed  ratio uncompressed_name
                 60                  31  -6.5% hello.sh

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

[[email protected] linux]$ gunzip -v hello.sh.gz 
hello.sh.gz:     -6.5% -- replaced with hello.sh
[[email protected] 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

顯然,校驗 md5 值,原檔案與 gunzip 和使用 gzip –d 解壓縮後的 md5 值一致。

通過上面的幾個示例,可以看出 gzip –d 等價於 gunzip 命令。