Linux命令 - gzip命令
Linux中 gzip 命令可以对文件进行压缩。gzip不仅可以用来压缩大的、较少使用的文件以节省磁盘空间,还可以和tar命令一起构成Linux操作系统中比较流行的压缩文件格式。据统计,gzip命令对文本文件有60%~70%的压缩率。
1.语法:
gzip [参数] [文件或者目录]
2.功能:
使用广泛的压缩程序,文件经它压缩过后,其名称后面会多出".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 显示版本信息。
-<压缩效率> 压缩效率是一个介于1-9的数值,预设值为"6",指定愈大的数值,压缩效率就会愈高。
--best 此参数的效果和指定"-9"参数相同,表示最慢压缩方法(高压缩比)。
--fast 此参数的效果和指定"-1"参数相同,表示最快压缩方法(低压缩比)。
4.常用范例:
例一:把test目录下的每个文件压缩成.gz文件
命令:gzip *
gzip压缩文件时不会保留原来的文件。
[root@localhost test]# ll
总用量 24
-rwxr--r--. 1 root root 96 5月 18 15:27 color.sh
-rwxr--r--. 1 root root 96 5月 18 10:15 lncolor
---xr--r--. 1 root root 13 5月 25 10:58 log1.txt
-rwxrw-r-x. 1 root root 592 5月 14 17:00 log2.txt
-rwxr-x--x. 1 root root 655 5月 14 17:06 log3.txt
-rwxr--r--. 1 root root 52 5月 25 10:58 log.txt
[root@localhost test]# gzip *
[root@localhost test]# ll
总用量 24
-rwxr--r--. 1 root root 78 5月 18 15:27 color.sh.gz
-rwxr--r--. 1 root root 77 5月 18 10:15 lncolor.gz
---xr--r--. 1 root root 42 5月 25 10:58 log1.txt.gz
-rwxrw-r-x. 1 root root 188 5月 14 17:00 log2.txt.gz
-rwxr-x--x. 1 root root 164 5月 14 17:06 log3.txt.gz
-rwxr--r--. 1 root root 63 5月 25 10:58 log.txt.gz
例二:显示压缩文件的信息
命令: gzip -l *
[root@localhost test]# gzip -l *
compressed uncompressed ratio uncompressed_name
78 96 46.9% color.sh
77 96 46.9% lncolor
42 13 -15.4% log1.txt
188 592 72.8% log2.txt
164 655 79.1% log3.txt
63 52 28.8% log.txt
612 1504 61.0% (totals)
例三:把每个压缩的文件解压并列出详细的信息
命令: gzip -dv *
[root@localhost test]# ll
总用量 24
-rwxr--r--. 1 root root 78 5月 18 15:27 color.sh.gz
-rwxr--r--. 1 root root 77 5月 18 10:15 lncolor.gz
---xr--r--. 1 root root 42 5月 25 10:58 log1.txt.gz
-rwxrw-r-x. 1 root root 188 5月 14 17:00 log2.txt.gz
-rwxr-x--x. 1 root root 164 5月 14 17:06 log3.txt.gz
-rwxr--r--. 1 root root 63 5月 25 10:58 log.txt.gz
[root@localhost test]# gzip -dv *
color.sh.gz: 46.9% -- replaced with color.sh
lncolor.gz: 46.9% -- replaced with lncolor
log1.txt.gz: -15.4% -- replaced with log1.txt
log2.txt.gz: 72.8% -- replaced with log2.txt
log3.txt.gz: 79.1% -- replaced with log3.txt
log.txt.gz: 28.8% -- replaced with log.txt
[root@localhost test]# ll
总用量 24
-rwxr--r--. 1 root root 96 5月 18 15:27 color.sh
-rwxr--r--. 1 root root 96 5月 18 10:15 lncolor
---xr--r--. 1 root root 13 5月 25 10:58 log1.txt
-rwxrw-r-x. 1 root root 592 5月 14 17:00 log2.txt
-rwxr-x--x. 1 root root 655 5月 14 17:06 log3.txt
-rwxr--r--. 1 root root 52 5月 25 10:58 log.txt
例四:递归的压缩目录
命令: gzip -rv test
所有test下面的文件都变成了*.gz,目录依然存在只是目录里面的文件相应变成了*.gz,这就是压缩,和打包不同。因为是对目录操作,所以需要加上-r选项,这样也可以对子目录进行递归了。
[root@localhost test]# ll
总用量 24
-rwxr--r--. 1 root root 96 5月 18 15:27 color.sh
-rwxr--r--. 1 root root 96 5月 18 10:15 lncolor
---xr--r--. 1 root root 13 5月 25 10:58 log1.txt
-rwxrw-r-x. 1 root root 592 5月 14 17:00 log2.txt
-rwxr-x--x. 1 root root 655 5月 14 17:06 log3.txt
-rwxr--r--. 1 root root 52 5月 25 10:58 log.txt
[root@localhost test]# cd ../
[root@localhost games]# gzip -rv test
test/color.sh: 46.9% -- replaced with test/color.sh.gz
test/lncolor: 46.9% -- replaced with test/lncolor.gz
test/log1.txt: -15.4% -- replaced with test/log1.txt.gz
test/log2.txt: 72.8% -- replaced with test/log2.txt.gz
test/log3.txt: 79.1% -- replaced with test/log3.txt.gz
test/log.txt: 28.8% -- replaced with test/log.txt.gz
[root@localhost games]# cd test/
[root@localhost test]# ll
总用量 24
-rwxr--r--. 1 root root 78 5月 18 15:27 color.sh.gz
-rwxr--r--. 1 root root 77 5月 18 10:15 lncolor.gz
---xr--r--. 1 root root 42 5月 25 10:58 log1.txt.gz
-rwxrw-r-x. 1 root root 188 5月 14 17:00 log2.txt.gz
-rwxr-x--x. 1 root root 164 5月 14 17:06 log3.txt.gz
-rwxr--r--. 1 root root 63 5月 25 10:58 log.txt.gz
[root@localhost test]# cd ../
[root@localhost games]# ll
总用量 0
drwxr-xr-x. 2 root root 118 5月 25 14:52 test
例五:递归的解压目录
命令: gzip -dr test
[root@localhost test]# ll
总用量 24
-rwxr--r--. 1 root root 78 5月 18 15:27 color.sh.gz
-rwxr--r--. 1 root root 77 5月 18 10:15 lncolor.gz
---xr--r--. 1 root root 42 5月 25 10:58 log1.txt.gz
-rwxrw-r-x. 1 root root 188 5月 14 17:00 log2.txt.gz
-rwxr-x--x. 1 root root 164 5月 14 17:06 log3.txt.gz
-rwxr--r--. 1 root root 63 5月 25 10:58 log.txt.gz
[root@localhost test]# cd ../
[root@localhost games]# gzip -dr test
[root@localhost games]# cd test/
[root@localhost test]# ll
总用量 24
-rwxr--r--. 1 root root 96 5月 18 15:27 color.sh
-rwxr--r--. 1 root root 96 5月 18 10:15 lncolor
---xr--r--. 1 root root 13 5月 25 10:58 log1.txt
-rwxrw-r-x. 1 root root 592 5月 14 17:00 log2.txt
-rwxr-x--x. 1 root root 655 5月 14 17:06 log3.txt
-rwxr--r--. 1 root root 52 5月 25 10:58 log.txt