linux每日命令(32):gzip命令

减小文件大小有两个明显的好处,一是能够减小存储空间,二是经过网络传输文件时,能够减小传输的时间。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文件

命令:ui

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文件code

2. 将当前目录下的每一个压缩的文件解压,并列出详细信息

命令:递归

gzip -dv *

输出:ip

[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目录下的每一个已压缩的文件进行解压ci

3. 详细当前目录下的压缩文件的信息,但不进行解压

命令:字符串

gzip -l *

输出:it

[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
相关文章
相关标签/搜索