linux 压缩工具gzip使用

工作原因使用centos比较多,自带压缩工具gzip。

使用gzip -h命令可以查看帮助文档。

Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).
Mandatory arguments to long options are mandatory for short options too.
  -c, --stdout      write on standard output, keep original files unchanged
  -d, --decompress  decompress
  -f, --force       force overwrite of output file and compress links
  -h, --help        give this help
  -l, --list        list compressed file contents
  -L, --license     display software license
  -n, --no-name     do not save or restore the original name and time stamp
  -N, --name        save or restore the original name and time stamp
  -q, --quiet       suppress all warnings
  -r, --recursive   operate recursively on directories
  -S, --suffix=SUF  use suffix SUF on compressed files
  -t, --test        test compressed file integrity
  -v, --verbose     verbose mode
  -V, --version     display version number
  -1, --fast        compress faster
  -9, --best        compress better
    --rsyncable   Make rsync-friendly archive

gzip只对单个文件进行压缩操作,无法对目录进行压缩;文件压缩完成后,会默认删除原文件,压缩文件后缀默认为.gz。如果需要对一个目录进行压缩,需要先使用tar工具将该目录打包成单个文件,然后再使用gzip压缩。

按照场景示例如下:

1、压缩单个文件

gzip filename

[root@localhost tmp]# ll
total 0
-rw-r--r-- 1 root root 0 May 17 16:58 test.txt
[root@localhost tmp]# gzip test.txt
[root@localhost tmp]# ll
total 4
-rw-r--r-- 1 root root 29 May 17 16:58 test.txt.gz

2、压缩目录

cn目录下有shanghai.txt 和guangzhou.txt两个文本,使用gzip命令是无法直接压缩cn目录的,需要先用tar将cn目录打包成单个文件,然后再压缩。

[root@localhost tmp]# tar -cvf cn.tar cn
cn/
cn/guangzhou.txt
cn/shanghai.txt
[root@localhost tmp]# ls
cn  cn.tar
[root@localhost tmp]# gzip cn.tar
[root@localhost tmp]# ls
cn  cn.tar.gz
[root@localhost tmp]# 

也可以通过tar参数控制在打包的同时已gzip压缩。tar -cvzf中的参数z代表以gzip压缩。

[root@localhost tmp]# tar -cvzf cn.tar.gz cn
cn/
cn/guangzhou.txt
cn/shanghai.txt
[root@localhost tmp]# ls
cn  cn.tar.gz
[root@localhost tmp]#

更多参考:

https://blog.csdn.net/eroswang/article/details/5555415/

https://www.cnblogs.com/chenmh/p/5882710.html

https://blog.csdn.net/belen_xue/article/details/51635460

https://www.cnblogs.com/alopex/p/7638346.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值