一、gzip命令
减少文件占用空间大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间。gzip
是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用。gzip
不仅可以用来压缩大的、较少使用的文件以节省磁盘空间,还可以和tar
命令一起构成Linux操作系统中比较流行的压缩文件格式。据统计,gzi
p命令对文本文件有60%~70%
的压缩率。
1.命令格式
gzip[参数][文件或者目录]
2.命令功能
gzip是个使用广泛的压缩程序,文件经它压缩过后,其名称后面会多出”.gz
“的扩展名。
3.命令参数
-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
。
4.使用实例
实例1:把test4目录下的每个文件压缩成.gz文件
命令:
gzip *
输出:
[tianyuanmengniu@localhost test4]$ ll
total 12
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 464 Feb 16 21:17 log1.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log2018.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log2019.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log2020.log
-rwxrwxr-x. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log2.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 384 Feb 16 21:17 log3.log
lrwxrwxrwx. 1 tianyuanmengniu tianyuanmengniu 7 Feb 16 21:17 log_link.log -> log.log
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 test3.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 1 Feb 16 21:17 tmpcore.log
[tianyuanmengniu@localhost test4]$ gzip *
gzip: log_link.log: Too many levels of symbolic links
[tianyuanmengniu@localhost test4]$ ll
total 36
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 267 Feb 16 21:17 log1.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 32 Feb 16 21:17 log2018.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 32 Feb 16 21:17 log2019.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 32 Feb 16 21:17 log2020.log.gz
-rwxrwxr-x. 1 tianyuanmengniu tianyuanmengniu 29 Feb 16 21:17 log2.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 220 Feb 16 21:17 log3.log.gz
lrwxrwxrwx. 1 tianyuanmengniu tianyuanmengniu 7 Feb 16 21:17 log_link.log -> log.log
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 28 Feb 16 21:17 log.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 30 Feb 16 21:17 test3.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 33 Feb 16 21:17 tmpcore.log.gz
[tianyuanmengniu@localhost test4]$
实例2:把例1中每个压缩的文件解压,并列出详细的信息
命令:
gzip -dv *
输出:
[tianyuanmengniu@localhost test4]$ ll
total 36
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 267 Feb 16 21:17 log1.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 32 Feb 16 21:17 log2018.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 32 Feb 16 21:17 log2019.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 32 Feb 16 21:17 log2020.log.gz
-rwxrwxr-x. 1 tianyuanmengniu tianyuanmengniu 29 Feb 16 21:17 log2.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 220 Feb 16 21:17 log3.log.gz
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 28 Feb 16 21:17 log.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 30 Feb 16 21:17 test3.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 33 Feb 16 21:17 tmpcore.log.gz
[tianyuanmengniu@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
[tianyuanmengniu@localhost test4]$
实例3:详细显示例1中每个压缩的文件的信息,并不解压
命令:
gzip -l *
输出:
[tianyuanmengniu@localhost test4]$ ll
total 36
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 267 Feb 16 21:17 log1.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 32 Feb 16 21:17 log2018.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 32 Feb 16 21:17 log2019.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 32 Feb 16 21:17 log2020.log.gz
-rwxrwxr-x. 1 tianyuanmengniu tianyuanmengniu 29 Feb 16 21:17 log2.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 220 Feb 16 21:17 log3.log.gz
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 28 Feb 16 21:17 log.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 30 Feb 16 21:17 test3.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 33 Feb 16 21:17 tmpcore.log.gz
[tianyuanmengniu@localhost test4]$ gzip -l
^C
[tianyuanmengniu@localhost test4]$ gzip -l *
compressed uncompressed ratio uncompressed_name
267 464 48.3% log1.log
32 0 0.0% log2018.log
32 0 0.0% log2019.log
32 0 0.0% log2020.log
29 0 0.0% log2.log
220 384 49.7% log3.log
28 0 0.0% log.log
30 0 0.0% test3.log
33 1 -200.0% tmpcore.log
703 849 20.7% (totals)
[tianyuanmengniu@localhost test4]$
实例4:压缩一个tar备份文件,此时压缩文件的扩展名为.tar.gz
命令:
gzip -r log.tar
输出:
[tianyuanmengniu@localhost test4]$ tar -cvf log.tar log.log
log.log
[tianyuanmengniu@localhost test4]$ ll
total 24
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 464 Feb 16 21:17 log1.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log2018.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log2019.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log2020.log
-rwxrwxr-x. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log2.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 384 Feb 16 21:17 log3.log
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 10240 Feb 16 21:25 log.tar
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 test3.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 1 Feb 16 21:17 tmpcore.log
[tianyuanmengniu@localhost test4]$ gzip -r log.tar
[tianyuanmengniu@localhost test4]$ ls -al log.tar.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 125 Feb 16 21:25 log.tar.gz
[tianyuanmengniu@localhost test4]$
实例5:递归的压缩目录
命令:
gzip -rv test4
输出:
[tianyuanmengniu@localhost ~]$ ll test4/
total 16
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 464 Feb 16 21:17 log1.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log2018.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log2019.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log2020.log
-rwxrwxr-x. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log2.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 384 Feb 16 21:17 log3.log
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 125 Feb 16 21:25 log.tar.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 test3.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 1 Feb 16 21:17 tmpcore.log
[tianyuanmengniu@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
[tianyuanmengniu@localhost ~]$ ll test4/
total 40
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 267 Feb 16 21:17 log1.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 32 Feb 16 21:17 log2018.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 32 Feb 16 21:17 log2019.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 32 Feb 16 21:17 log2020.log.gz
-rwxrwxr-x. 1 tianyuanmengniu tianyuanmengniu 29 Feb 16 21:17 log2.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 220 Feb 16 21:17 log3.log.gz
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 28 Feb 16 21:17 log.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 125 Feb 16 21:25 log.tar.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 30 Feb 16 21:17 test3.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 33 Feb 16 21:17 tmpcore.log.gz
[tianyuanmengniu@localhost ~]$
说明:这样,所有
test
下面的文件都变成了*.gz
,目录依然存在只是目录里面的文件相应变成了*.gz
。这就是压缩,和打包不同。因为是对目录操作,所以需要加上-r
选项,这样也可以对子目录进行递归了。
实例6:递归地解压目录
命令:
gzip -dr test4
输出:
[tianyuanmengniu@localhost ~]$ ll test4/
total 40
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 267 Feb 16 21:17 log1.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 32 Feb 16 21:17 log2018.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 32 Feb 16 21:17 log2019.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 32 Feb 16 21:17 log2020.log.gz
-rwxrwxr-x. 1 tianyuanmengniu tianyuanmengniu 29 Feb 16 21:17 log2.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 220 Feb 16 21:17 log3.log.gz
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 28 Feb 16 21:17 log.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 125 Feb 16 21:25 log.tar.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 30 Feb 16 21:17 test3.log.gz
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 33 Feb 16 21:17 tmpcore.log.gz
[tianyuanmengniu@localhost ~]$ gzip -rd test4
[tianyuanmengniu@localhost ~]$ ll test4
total 24
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 464 Feb 16 21:17 log1.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log2018.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log2019.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log2020.log
-rwxrwxr-x. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log2.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 384 Feb 16 21:17 log3.log
-rwxr-xr-x. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 log.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 10240 Feb 16 21:25 log.tar
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 0 Feb 16 21:17 test3.log
-rw-rw-r--. 1 tianyuanmengniu tianyuanmengniu 1 Feb 16 21:17 tmpcore.log
[tianyuanmengniu@localhost ~]$