减少文件占用空间大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间。gzip
是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用。gzip
不仅可以用来压缩大的、较少使用的文件以节省磁盘空间,还可以和tar
命令一起构成Linux操作系统中比较流行的压缩文件格式。据统计,gzi
p命令对文本文件有60%~70%
的压缩率。
gzip[参数][文件或者目录]
gzip是个使用广泛的压缩程序,文件经它压缩过后,其名称后面会多出”.gz
“的扩展名。
-a
或--ascii
使用ASCII文字模式。 -c
或--stdout
或--to-stdout
把压缩后的文件输出到标准输出设备,不去更动原始文件。 -d
或--decompress
或----uncompress
解开压缩文件。 -f
或--force
强行压缩文件。不理会文件名称或硬连接是否存在以及该文件是否为符号连接。 -h
或--help
在线帮助。 -l
或--list
列出压缩文件的相关信息。 -L
或--license
显示版本与版权信息。 -n
或--no-name
压缩文件时,不保存原来的文件名称及时间戳记。 -N
或--name
压缩文件时,保存原来的文件名称及时间戳记。 -q
或--quiet
不显示警告信息。 -r
或--recursive
递归处理,将指定目录下的所有文件及子目录一并处理。 -S
<压缩字尾字符串>或----suffix
<压缩字尾字符串> 更改压缩字尾字符串。 -t
或--test
测试压缩文件是否正确无误。 -v
或--verbose
显示指令执行过程。 -V
或--version
显示版本信息。 -num
用指定的数字num
调整压缩的速度,-1
或--fast
表示最快压缩方法(低压缩比),-9
或--best
表示最慢压缩方法(高压缩比)。系统缺省值为6
。 命令:
gzip *
输出:
[zyiz@localhost test4]$ ll total 12 -rwxr-xr-x. 1 zyiz zyiz 464 Feb 16 21:17 log1.log -rw-rw-r--. 1 zyiz zyiz 0 Feb 16 21:17 log2018.log -rw-rw-r--. 1 zyiz zyiz 0 Feb 16 21:17 log2019.log -rw-rw-r--. 1 zyiz zyiz 0 Feb 16 21:17 log2020.log -rwxrwxr-x. 1 zyiz zyiz 0 Feb 16 21:17 log2.log -rw-rw-r--. 1 zyiz zyiz 384 Feb 16 21:17 log3.log lrwxrwxrwx. 1 zyiz zyiz 7 Feb 16 21:17 log_link.log -> log.log -rwxr-xr-x. 1 zyiz zyiz 0 Feb 16 21:17 log.log -rw-rw-r--. 1 zyiz zyiz 0 Feb 16 21:17 test3.log -rw-rw-r--. 1 zyiz zyiz 1 Feb 16 21:17 tmpcore.log [zyiz@localhost test4]$ gzip * gzip: log_link.log: Too many levels of symbolic links [zyiz@localhost test4]$ ll total 36 -rwxr-xr-x. 1 zyiz zyiz 267 Feb 16 21:17 log1.log.gz -rw-rw-r--. 1 zyiz zyiz 32 Feb 16 21:17 log2018.log.gz -rw-rw-r--. 1 zyiz zyiz 32 Feb 16 21:17 log2019.log.gz -rw-rw-r--. 1 zyiz zyiz 32 Feb 16 21:17 log2020.log.gz -rwxrwxr-x. 1 zyiz zyiz 29 Feb 16 21:17 log2.log.gz -rw-rw-r--. 1 zyiz zyiz 220 Feb 16 21:17 log3.log.gz lrwxrwxrwx. 1 zyiz zyiz 7 Feb 16 21:17 log_link.log -> log.log -rwxr-xr-x. 1 zyiz zyiz 28 Feb 16 21:17 log.log.gz -rw-rw-r--. 1 zyiz zyiz 30 Feb 16 21:17 test3.log.gz -rw-rw-r--. 1 zyiz zyiz 33 Feb 16 21:17 tmpcore.log.gz [zyiz@localhost test4]$
命令:
gzip -dv *
输出:
[zyiz@localhost test4]$ ll total 36 -rwxr-xr-x. 1 zyiz zyiz 267 Feb 16 21:17 log1.log.gz -rw-rw-r--. 1 zyiz zyiz 32 Feb 16 21:17 log2018.log.gz -rw-rw-r--. 1 zyiz zyiz 32 Feb 16 21:17 log2019.log.gz -rw-rw-r--. 1 zyiz zyiz 32 Feb 16 21:17 log2020.log.gz -rwxrwxr-x. 1 zyiz zyiz 29 Feb 16 21:17 log2.log.gz -rw-rw-r--. 1 zyiz zyiz 220 Feb 16 21:17 log3.log.gz -rwxr-xr-x. 1 zyiz zyiz 28 Feb 16 21:17 log.log.gz -rw-rw-r--. 1 zyiz zyiz 30 Feb 16 21:17 test3.log.gz -rw-rw-r--. 1 zyiz zyiz 33 Feb 16 21:17 tmpcore.log.gz [zyiz@localhost test4]$ gzip -dv * log1.log.gz: 48.3% -- replaced with log1.log log2018.log.gz: 0.0% -- replaced with log2018.log log2019.log.gz: 0.0% -- replaced with log2019.log log2020.log.gz: 0.0% -- replaced with log2020.log log2.log.gz: 0.0% -- replaced with log2.log log3.log.gz: 49.7% -- replaced with log3.log log.log.gz: 0.0% -- replaced with log.log test3.log.gz: 0.0% -- replaced with test3.log tmpcore.log.gz: -200.0% -- replaced with tmpcore.log [zyiz@localhost test4]$
命令:
gzip -l *
输出:
[zyiz@localhost test4]$ ll total 36 -rwxr-xr-x. 1 zyiz zyiz 267 Feb 16 21:17 log1.log.gz -rw-rw-r--. 1 zyiz zyiz 32 Feb 16 21:17 log2018.log.gz -rw-rw-r--. 1 zyiz zyiz 32 Feb 16 21:17 log2019.log.gz -rw-rw-r--. 1 zyiz zyiz 32 Feb 16 21:17 log2020.log.gz -rwxrwxr-x. 1 zyiz zyiz 29 Feb 16 21:17 log2.log.gz -rw-rw-r--. 1 zyiz zyiz 220 Feb 16 21:17 log3.log.gz -rwxr-xr-x. 1 zyiz zyiz 28 Feb 16 21:17 log.log.gz -rw-rw-r--. 1 zyiz zyiz 30 Feb 16 21:17 test3.log.gz -rw-rw-r--. 1 zyiz zyiz 33 Feb 16 21:17 tmpcore.log.gz [zyiz@localhost test4]$ gzip -l ^C [zyiz@localhost test4]$ gzip -l * compressed uncompressed ratio uncompressed_name 48.3% log1.log 0.0% log2018.log 0.0% log2019.log 0.0% log2020.log 0.0% log2.log 49.7% log3.log 0.0% log.log 0.0% test3.log -200.0% tmpcore.log 20.7% (totals) [zyiz@localhost test4]$
命令:
gzip -r log.tar
输出:
[zyiz@localhost test4]$ tar -cvf log.tar log.log log.log [zyiz@localhost test4]$ ll total 24 -rwxr-xr-x. 1 zyiz zyiz 464 Feb 16 21:17 log1.log -rw-rw-r--. 1 zyiz zyiz 0 Feb 16 21:17 log2018.log -rw-rw-r--. 1 zyiz zyiz 0 Feb 16 21:17 log2019.log -rw-rw-r--. 1 zyiz zyiz 0 Feb 16 21:17 log2020.log -rwxrwxr-x. 1 zyiz zyiz 0 Feb 16 21:17 log2.log -rw-rw-r--. 1 zyiz zyiz 384 Feb 16 21:17 log3.log -rwxr-xr-x. 1 zyiz zyiz 0 Feb 16 21:17 log.log -rw-rw-r--. 1 zyiz zyiz 10240 Feb 16 21:25 log.tar -rw-rw-r--. 1 zyiz zyiz 0 Feb 16 21:17 test3.log -rw-rw-r--. 1 zyiz zyiz 1 Feb 16 21:17 tmpcore.log [zyiz@localhost test4]$ gzip -r log.tar [zyiz@localhost test4]$ ls -al log.tar.gz -rw-rw-r--. 1 zyiz zyiz 125 Feb 16 21:25 log.tar.gz [zyiz@localhost test4]$
命令:
gzip -rv test4
输出:
[zyiz@localhost ~]$ ll test4/ total 16 -rwxr-xr-x. 1 zyiz zyiz 464 Feb 16 21:17 log1.log -rw-rw-r--. 1 zyiz zyiz 0 Feb 16 21:17 log2018.log -rw-rw-r--. 1 zyiz zyiz 0 Feb 16 21:17 log2019.log -rw-rw-r--. 1 zyiz zyiz 0 Feb 16 21:17 log2020.log -rwxrwxr-x. 1 zyiz zyiz 0 Feb 16 21:17 log2.log -rw-rw-r--. 1 zyiz zyiz 384 Feb 16 21:17 log3.log -rwxr-xr-x. 1 zyiz zyiz 0 Feb 16 21:17 log.log -rw-rw-r--. 1 zyiz zyiz 125 Feb 16 21:25 log.tar.gz -rw-rw-r--. 1 zyiz zyiz 0 Feb 16 21:17 test3.log -rw-rw-r--. 1 zyiz zyiz 1 Feb 16 21:17 tmpcore.log [zyiz@localhost ~]$ gzip -rv test4/ test4//log1.log: 48.3% -- replaced with test4//log1.log.gz test4//log2018.log: 0.0% -- replaced with test4//log2018.log.gz test4//log2019.log: 0.0% -- replaced with test4//log2019.log.gz test4//log2020.log: 0.0% -- replaced with test4//log2020.log.gz test4//log2.log: 0.0% -- replaced with test4//log2.log.gz test4//log3.log: 49.7% -- replaced with test4//log3.log.gz test4//log.log: 0.0% -- replaced with test4//log.log.gz test4//test3.log: 0.0% -- replaced with test4//test3.log.gz test4//tmpcore.log: -200.0% -- replaced with test4//tmpcore.log.gz gzip: test4//log.tar.gz already has .gz suffix -- unchanged [zyiz@localhost ~]$ ll test4/ total 40 -rwxr-xr-x. 1 zyiz zyiz 267 Feb 16 21:17 log1.log.gz -rw-rw-r--. 1 zyiz zyiz 32 Feb 16 21:17 log2018.log.gz -rw-rw-r--. 1 zyiz zyiz 32 Feb 16 21:17 log2019.log.gz -rw-rw-r--. 1 zyiz zyiz 32 Feb 16 21:17 log2020.log.gz -rwxrwxr-x. 1 zyiz zyiz 29 Feb 16 21:17 log2.log.gz -rw-rw-r--. 1 zyiz zyiz 220 Feb 16 21:17 log3.log.gz -rwxr-xr-x. 1 zyiz zyiz 28 Feb 16 21:17 log.log.gz -rw-rw-r--. 1 zyiz zyiz 125 Feb 16 21:25 log.tar.gz -rw-rw-r--. 1 zyiz zyiz 30 Feb 16 21:17 test3.log.gz -rw-rw-r--. 1 zyiz zyiz 33 Feb 16 21:17 tmpcore.log.gz [zyiz@localhost ~]$
说明:这样,所有
test
下面的文件都变成了*.gz
,目录依然存在只是目录里面的文件相应变成了*.gz
。这就是压缩,和打包不同。因为是对目录操作,所以需要加上-r
选项,这样也可以对子目录进行递归了。
命令:
gzip -dr test4
输出:
[zyiz@localhost ~]$ ll test4/ total 40 -rwxr-xr-x. 1 zyiz zyiz 267 Feb 16 21:17 log1.log.gz -rw-rw-r--. 1 zyiz zyiz 32 Feb 16 21:17 log2018.log.gz -rw-rw-r--. 1 zyiz zyiz 32 Feb 16 21:17 log2019.log.gz -rw-rw-r--. 1 zyiz zyiz 32 Feb 16 21:17 log2020.log.gz -rwxrwxr-x. 1 zyiz zyiz 29 Feb 16 21:17 log2.log.gz -rw-rw-r--. 1 zyiz zyiz 220 Feb 16 21:17 log3.log.gz -rwxr-xr-x. 1 zyiz zyiz 28 Feb 16 21:17 log.log.gz -rw-rw-r--. 1 zyiz zyiz 125 Feb 16 21:25 log.tar.gz -rw-rw-r--. 1 zyiz zyiz 30 Feb 16 21:17 test3.log.gz -rw-rw-r--. 1 zyiz zyiz 33 Feb 16 21:17 tmpcore.log.gz [zyiz@localhost ~]$ gzip -rd test4 [zyiz@localhost ~]$ ll test4 total 24 -rwxr-xr-x. 1 zyiz zyiz 464 Feb 16 21:17 log1.log -rw-rw-r--. 1 zyiz zyiz 0 Feb 16 21:17 log2018.log -rw-rw-r--. 1 zyiz zyiz 0 Feb 16 21:17 log2019.log -rw-rw-r--. 1 zyiz zyiz 0 Feb 16 21:17 log2020.log -rwxrwxr-x. 1 zyiz zyiz 0 Feb 16 21:17 log2.log -rw-rw-r--. 1 zyiz zyiz 384 Feb 16 21:17 log3.log -rwxr-xr-x. 1 zyiz zyiz 0 Feb 16 21:17 log.log -rw-rw-r--. 1 zyiz zyiz 10240 Feb 16 21:25 log.tar -rw-rw-r--. 1 zyiz zyiz 0 Feb 16 21:17 test3.log -rw-rw-r--. 1 zyiz zyiz 1 Feb 16 21:17 tmpcore.log [zyiz@localhost ~]$