天天看点

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      

继续阅读