工作原因使用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