Linux文件打包及下载命令-gzip命令

一、gzip命令

减少文件占用空间大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间。gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用。gzip不仅可以用来压缩大的、较少使用的文件以节省磁盘空间,还可以和tar命令一起构成Linux操作系统中比较流行的压缩文件格式。据统计,gzip命令对文本文件有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 ~]$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值