减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间。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
或----suffix ,更改压缩字尾字符串。
-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 -drvtest
输出:
[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