减少文件大小有两个明显的好处, 一是可以减少存储空间, 二是通过网络传输文件时, 可以减少传输的时间. gzip 是在 Linux 系统中经常使用的一个对文件进行压缩和解压缩的命令, 既方便又好用. gzip 不仅可以用来压缩大的, 较少使用的文件以节省磁盘空间, 还可以和 tar 命令一起构成 Linux 操作系统中比较流行的压缩文件格式. 据统计, gzip 命令对文本文件有 60%~70% 的压缩率.
一.命令格式
gzip [参数] [文件或者目录]
二. 命令功能
gzip 是个使用广泛的压缩程序, 文件经它压缩过后, 其名称后面会多出 ".gz" 的扩展名
三. 命令参数
必要参数
参数 | 描述 |
---|---|
-a | 或 --ascii , 使用 ASCII 文字模式。 |
-c | 或 --stdout 或 --to-stdout ,把压缩后的文件输出到标准输出设备,不去变动原始文件。 |
-d | 或 --decompress 或 ----uncompress,解开压缩文件 |
-r | 或 --recursive , 递归处理,将指定目录下的所有文件及子目录一并处理。 |
-f | 或 --force , 强行压缩文件。不理会文件名称或硬连接是否存在以及该文件是否为符号连接。 |
-l | 或 --list, 列出压缩文件的相关信息。 |
-n | 或 --no-name , 压缩文件时,不保存原来的文件名称及时间戳记。 |
-N | 或 --name ,压缩文件时,保存原来的文件名称及时间戳记。 |
-q | 或 --quiet , 不显示警告信息。 |
-S< 压缩字尾字符串 & gt; | 或 ----suffix< 压缩字尾字符串 & gt; ,更改压缩字尾字符串。 |
-t | 或 --test , 测试压缩文件是否正确无误。 |
-v | 或 --verbose ,显示指令执行过程 |
-V | 或 --version , 显示版本信息。 |
-L | 或 --license 显示版本与版权信息。 |
-num | 用指定的数字 num 调整压缩的速度,-1 或 --fast 表示最快压缩方法(低压缩比),-9 或 --best 表示最慢压缩方法(高压缩比)。系统缺省值为 6。 |
-h | 或 --help , 在线帮助。 |
四. 使用实例
1: 将当前目录下的每个文件压缩成. gz 文件
命令:
gzip *
输出:
- [root@localhost test]# ll
- total 12
-rw-r--r-- 1 root root 2117 12 月 1 09:08 1.log
-rw-r--r-- 1 root root 2199 12 月 1 09:08 2.log
-rw-r--r-- 1 root root 2117 12 月 1 09:08 3.log
- [root@localhost test]# gzip *
- [root@localhost test]# ll
- total 12
-rw-r--r-- 1 root root 1189 12 月 1 09:08 1.log.gz
-rw-r--r-- 1 root root 1245 12 月 1 09:08 2.log.gz
-rw-r--r-- 1 root root 1189 12 月 1 09:08 3.log.gz
说明:
将 test 目录下的每个文件压缩成. gz 文件
2. 将当前目录下的每个压缩的文件解压, 并列出详细信息
命令:
gzip -dv *
输出:
- [root@localhost test]# touch 4.log
- [root@localhost test]# ll
- total 12
-rw-r--r-- 1 root root 1189 12 月 1 09:08 1.log.gz
-rw-r--r-- 1 root root 1245 12 月 1 09:08 2.log.gz
-rw-r--r-- 1 root root 1189 12 月 1 09:08 3.log.gz
-rw-r--r-- 1 root root 0 12 月 1 09:17 4.log
- [root@localhost test]# gzip -dv *
- 1.log.gz: 45.0% -- replaced with 1.log
- 2.log.gz: 44.5% -- replaced with 2.log
- 3.log.gz: 45.0% -- replaced with 3.log
- gzip: 4.log: unknown suffix -- ignored
- [root@localhost test]# ll
- total 12
-rw-r--r-- 1 root root 2117 12 月 1 09:08 1.log
-rw-r--r-- 1 root root 2199 12 月 1 09:08 2.log
-rw-r--r-- 1 root root 2117 12 月 1 09:08 3.log
-rw-r--r-- 1 root root 0 12 月 1 09:17 4.log
说明:
将 test 目录下的每个已压缩的文件进行解压
3. 详细当前目录下的压缩文件的信息, 但不进行解压
命令:
gzip -l *
输出:
- [root@localhost test]# ll
- total 12
-rw-r--r-- 1 root root 1189 12 月 1 09:08 1.log.gz
-rw-r--r-- 1 root root 1245 12 月 1 09:08 2.log.gz
-rw-r--r-- 1 root root 1189 12 月 1 09:08 3.log.gz
- [root@localhost test]# gzip -l *
- compressed uncompressed ratio uncompressed_name
- 1189 2117 45.0% 1.log
- 1245 2199 44.5% 2.log
- 1189 2117 45.0% 3.log
- 3623 6433 44.1% (totals)
说明:
详细显示例 1 中的每个压缩文件的信息, 但不进行解压
4. 递归的压缩目录
命令:
gzip -rv test
输出:
- [root@localhost test]# ll
- total 12
-rw-r--r-- 1 root root 2117 12 月 1 09:08 1.log
-rw-r--r-- 1 root root 2199 12 月 1 09:08 2.log
-rw-r--r-- 1 root root 2117 12 月 1 09:08 3.log
- [root@localhost test]# cd ..
- [root@localhost hc]# gzip -v test
- gzip: test is a directory -- ignored
- [root@localhost hc]# gzip -rv test
- test/1.log: 45.0% -- replaced with test/1.log.gz
- test/2.log: 44.5% -- replaced with test/2.log.gz
- test/3.log: 45.0% -- replaced with test/3.log.gz
- [root@localhost hc]# cd test
- [root@localhost test]# ll
- total 12
-rw-r--r-- 1 root root 1189 12 月 1 09:08 1.log.gz
-rw-r--r-- 1 root root 1245 12 月 1 09:08 2.log.gz
-rw-r--r-- 1 root root 1189 12 月 1 09:08 3.log.gz
说明:
这样所有 test 下面的文件都变成了 *.gz, 目录依然存在只是目录里面的文件相应变成了 *.gz, 这就是压缩, 和打包不同. 因为是对目录操作, 所以需要加上 - r 选项, 这样也可以对子目录进行递归了.
如果要压缩成一个 gz 文件, 可以先用 tar 命令对目录进行打包, 然后再对打包文件使用 gzip 命令
5. 递归的解压目录
命令:
gzip -drv test
输出:
- [root@localhost test]# ll
- total 12
-rw-r--r-- 1 root root 1189 12 月 1 09:08 1.log.gz
-rw-r--r-- 1 root root 1245 12 月 1 09:08 2.log.gz
-rw-r--r-- 1 root root 1189 12 月 1 09:08 3.log.gz
- [root@localhost test]# cd ..
- [root@localhost hc]# gzip -drv test
- test/1.log.gz: 45.0% -- replaced with test/1.log
- test/2.log.gz: 44.5% -- replaced with test/2.log
- test/3.log.gz: 45.0% -- replaced with test/3.log
- [root@localhost hc]# cd test
- [root@localhost test]# ll
- total 12
-rw-r--r-- 1 root root 2117 12 月 1 09:08 1.log
-rw-r--r-- 1 root root 2199 12 月 1 09:08 2.log
-rw-r--r-- 1 root root 2117 12 月 1 09:08 3.log
来源: https://www.cnblogs.com/huchong/p/10048301.html