Linux操作系统的压缩、解压缩工具介绍
Linux操作系统的压缩、解压缩工具介绍
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.compress/uncompress命令常用参数
Linux compress命令: 是一个相当古老的 unix 档案压缩指令,压缩后的档案会加上一个 .Z 延伸档名以区别未压缩的档案,压缩后的档案可以以 uncompress 解压。若要将数个档案压成一个压缩档,必须先将档案 tar 起来再压缩。由于 gzip 可以产生更理想的压缩比例,一般人多已改用 gzip 为档案压缩工具。 compress [-dfvcVr] [-b maxbits] [file ...] -d: 解压缩,相当于uncompress -c: 结果输出至标准输出,不删除原文件
-v:
显示详情 uncompress 解压缩 zcat file.Z >file
1>.安装commpress/uncompress软件包
[root@node101.yinzhengjie.org.cn ~]# yum -y install ncompress.x86_64 Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.aliyun.com * updates: mirror.bit.edu.cn Resolving Dependencies --> Running transaction check ---> Package ncompress.x86_64 0:4.2.4.4-3.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved =================================================================================================================================== Package Arch Version Repository Size =================================================================================================================================== Installing: ncompress x86_64 4.2.4.4-3.el7 base 26 k Transaction Summary =================================================================================================================================== Install 1 Package Total download size: 26 k Installed size: 35 k Downloading packages: ncompress-4.2.4.4-3.el7.x86_64.rpm | 26 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : ncompress-4.2.4.4-3.el7.x86_64 1/1 Verifying : ncompress-4.2.4.4-3.el7.x86_64 1/1 Installed: ncompress.x86_64 0:4.2.4.4-3.el7 Complete! [root@node101.yinzhengjie.org.cn ~]#
2>.压缩文件
[root@node101.yinzhengjie.org.cn ~]# ll /var/log/messages -h -rw------- 1 root root 89K Dec 13 17:20 /var/log/messages [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# for _ in `seq 1000`;do cat /var/log/messages >> /root/message;done #创建测试压缩的文件 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 87M -rw-r--r-- 1 root root 87M Dec 13 17:45 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# compress message #compress命令压缩文件后会将源文件删除,并在源文件名称后缀中多了一个".Z" [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 18M -rw-r--r-- 1 root root 18M Dec 13 17:45 message.Z #文件的确是变小啦。 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
3>.解压文件
[root@node101.yinzhengjie.org.cn ~]# ll -h total 18M -rw-r--r-- 1 root root 18M Dec 13 17:45 message.Z [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# uncompress message.Z [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 87M -rw-r--r-- 1 root root 87M Dec 13 17:45 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
4>."-c"参数使用案例
[root@node101.yinzhengjie.org.cn ~]# ll -h total 87M -rw-r--r-- 1 root root 87M Dec 13 17:45 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# compress -c message > message.Z #不建议将压缩结果直接输出到标准输出,因为打印在标准输出咱们人为是读不懂的,因此可以考虑重定向,这样即压缩来源文件,也达到来不删除源文件的好处。 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 104M -rw-r--r-- 1 root root 87M Dec 13 17:45 message -rw-r--r-- 1 root root 18M Dec 13 18:07 message.Z [root@node101.yinzhengjie.org.cn ~]#
5>.zcat使用案例
[root@node101.yinzhengjie.org.cn ~]# ll total 106344 -rw-r--r-- 1 root root 90740000 Dec 13 17:45 message -rw-r--r-- 1 root root 18150727 Dec 13 18:07 message.Z [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# zcat message.Z #预览压缩内容但并不解压 ...... Dec 13 15:01:01 node101 systemd: Started Session 55 of user root. Dec 13 15:01:01 node101 systemd: Removed slice User Slice of root. Dec 13 15:41:07 node101 systemd: Starting Cleanup of Temporary Directories... Dec 13 15:41:07 node101 systemd: Started Cleanup of Temporary Directories. Dec 13 16:01:01 node101 systemd: Created slice User Slice of root. Dec 13 16:01:01 node101 systemd: Started Session 56 of user root. Dec 13 16:01:01 node101 systemd: Removed slice User Slice of root. Dec 13 16:39:05 node101 sshd[333]: Accepted password for root from 172.30.1.2 port 50754 ssh2 Dec 13 16:39:05 node101 systemd: Created slice User Slice of root. Dec 13 16:39:05 node101 systemd-logind: New session 57 of user root. Dec 13 16:39:05 node101 systemd: Started Session 57 of user root. Dec 13 16:42:32 node101 chronyd[3582]: Source 116.203.151.74 replaced with 119.28.183.184 Dec 13 16:45:45 node101 chronyd[3582]: Selected source 119.28.183.184 Dec 13 16:45:45 node101 chronyd[3582]: System clock wrong by 165882.562596 seconds, adjustment started Dec 13 17:01:01 node101 systemd: Started Session 58 of user root. Dec 13 17:19:27 node101 chronyd[3582]: Source 144.76.76.107 replaced with 193.182.111.141 Dec 13 17:20:27 node101 sshd[2691]: Accepted password for root from 172.30.1.2 port 51751 ssh2 Dec 13 17:20:27 node101 systemd-logind: New session 59 of user root. Dec 13 17:20:27 node101 systemd: Started Session 59 of user root. [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 106344 -rw-r--r-- 1 root root 90740000 Dec 13 17:45 message -rw-r--r-- 1 root root 18150727 Dec 13 18:07 message.Z [root@node101.yinzhengjie.org.cn ~]#
6>.compress也支持交互式压缩
[root@node101.yinzhengjie.org.cn ~]# ll total 106344 -rw-r--r-- 1 root root 90740000 Dec 13 17:45 message -rw-r--r-- 1 root root 18150727 Dec 13 18:07 message.Z [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# compress > test.Z #按住"ctrl+d"就会结束命令行输出 88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888[root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 106348 -rw-r--r-- 1 root root 90740000 Dec 13 17:45 message -rw-r--r-- 1 root root 18150727 Dec 13 18:07 message.Z -rw-r--r-- 1 root root 32 Dec 13 18:13 test.Z [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# zcat test.Z | wc -c #重复的次数越多,压缩比例就越高 320 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 106348 -rw-r--r-- 1 root root 90740000 Dec 13 17:45 message -rw-r--r-- 1 root root 18150727 Dec 13 18:07 message.Z -rw-r--r-- 1 root root 32 Dec 13 18:13 test.Z [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
7>."-d"参数介绍
[root@node101.yinzhengjie.org.cn ~]# ll total 106348 -rw-r--r-- 1 root root 90740000 Dec 13 17:45 message -rw-r--r-- 1 root root 18150727 Dec 13 18:07 message.Z -rw-r--r-- 1 root root 32 Dec 13 18:13 test.Z [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# compress -d test.Z #解压"test.Z"文件 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 106348 -rw-r--r-- 1 root root 90740000 Dec 13 17:45 message -rw-r--r-- 1 root root 18150727 Dec 13 18:07 message.Z -rw-r--r-- 1 root root 320 Dec 13 18:13 test [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
8>.compress命令支持将命令标准输出进行压缩
[root@node101.yinzhengjie.org.cn ~]# dmesg | compress > dmesg.Z [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 24 -rw-r--r-- 1 root root 21010 Dec 13 18:22 dmesg.Z [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# uncompress dmesg.Z [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 48 -rw-r--r-- 1 root root 46804 Dec 13 18:22 dmesg [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
9>.更多compress压缩命令帮助信息
[root@node101.yinzhengjie.org.cn ~]# compress --help Unknown flag: '-'; Usage: compress [-dfvcVr] [-b maxbits] [file ...] -d If given, decompression is done instead. -c Write output on stdout, don't remove original. -b Parameter limits the max number of bits/code. -f Forces output file to be generated, even if one already. exists, and even if no space is saved by compressing. If -f is not used, the user will be prompted if stdin is. a tty, otherwise, the output file will not be overwritten. -v Write compression statistics. -V Output vesion and compile options. -r Recursive. If a filename is a directory, descend into it and compress everything in it. [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# man compress
二.gzip/gunzip(gzip完胜compress)
gzip [OPTION]... FILE ...
-d:
解压缩,相当于gunzip
-c:
结果输出至标准输出,保留原文件不改变
-num:
这个"num"对应的数字范围是:1-9,指定压缩比,值越大压缩比越大,默认压缩比例是6,可参考下面的实验结果。
zcat:
不显式解压缩的前提下查看文本文件内容
1>.压缩同样文件对比compress和gzip的压缩比例
[root@node101.yinzhengjie.org.cn ~]# for _ in `seq 10000`;do cat /var/log/messages >> /root/message;done [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 877M -rw-r--r-- 1 root root 877M Dec 14 01:57 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cp message f1.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cp message f2.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 2.6G -rw-r--r-- 1 root root 877M Dec 14 01:59 f1.txt -rw-r--r-- 1 root root 877M Dec 14 01:59 f2.txt -rw-r--r-- 1 root root 877M Dec 14 01:57 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# compress f1.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# gzip f2.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 1.2G -rw-r--r-- 1 root root 171M Dec 14 01:59 f1.txt.Z -rw-r--r-- 1 root root 164M Dec 14 01:59 f2.txt.gz -rw-r--r-- 1 root root 877M Dec 14 01:57 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
2>.解压文件
[root@node101.yinzhengjie.org.cn ~]# ll -h total 1.2G -rw-r--r-- 1 root root 171M Dec 14 01:59 f1.txt.Z -rw-r--r-- 1 root root 164M Dec 14 01:59 f2.txt.gz -rw-r--r-- 1 root root 877M Dec 14 01:57 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# gunzip f2.txt.gz [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 1.9G -rw-r--r-- 1 root root 171M Dec 14 01:59 f1.txt.Z -rw-r--r-- 1 root root 877M Dec 14 01:59 f2.txt -rw-r--r-- 1 root root 877M Dec 14 01:57 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
3>.指定压缩级别(默认压缩级别在6左右)
[root@node101.yinzhengjie.org.cn ~]# ll -h total 768M -rw-r--r--. 1 root root 768M Dec 16 18:18 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cp message f1.txt [root@node101.yinzhengjie.org.cn ~]# cp message f2.txt [root@node101.yinzhengjie.org.cn ~]# cp message f3.txt [root@node101.yinzhengjie.org.cn ~]# cp message f4.txt [root@node101.yinzhengjie.org.cn ~]# cp message f5.txt [root@node101.yinzhengjie.org.cn ~]# cp message f6.txt [root@node101.yinzhengjie.org.cn ~]# cp message f7.txt [root@node101.yinzhengjie.org.cn ~]# cp message f8.txt [root@node101.yinzhengjie.org.cn ~]# cp message f9.txt [root@node101.yinzhengjie.org.cn ~]# cp message f10.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 8.3G -rw-r--r--. 1 root root 768M Dec 16 18:21 f10.txt -rw-r--r--. 1 root root 768M Dec 16 18:19 f1.txt -rw-r--r--. 1 root root 768M Dec 16 18:19 f2.txt -rw-r--r--. 1 root root 768M Dec 16 18:19 f3.txt -rw-r--r--. 1 root root 768M Dec 16 18:19 f4.txt -rw-r--r--. 1 root root 768M Dec 16 18:20 f5.txt -rw-r--r--. 1 root root 768M Dec 16 18:20 f6.txt -rw-r--r--. 1 root root 768M Dec 16 18:20 f7.txt -rw-r--r--. 1 root root 768M Dec 16 18:20 f8.txt -rw-r--r--. 1 root root 768M Dec 16 18:21 f9.txt -rw-r--r--. 1 root root 768M Dec 16 18:18 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# gzip -1 f1.txt [root@node101.yinzhengjie.org.cn ~]# gzip -2 f2.txt [root@node101.yinzhengjie.org.cn ~]# gzip -3 f3.txt [root@node101.yinzhengjie.org.cn ~]# gzip -4 f4.txt [root@node101.yinzhengjie.org.cn ~]# gzip -5 f5.txt [root@node101.yinzhengjie.org.cn ~]# gzip -6 f6.txt [root@node101.yinzhengjie.org.cn ~]# gzip -7 f7.txt [root@node101.yinzhengjie.org.cn ~]# gzip -8 f8.txt [root@node101.yinzhengjie.org.cn ~]# gzip -9 f9.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# gzip f10.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 2.3G -rw-r--r--. 1 root root 149M Dec 16 18:21 f10.txt.gz -rw-r--r--. 1 root root 173M Dec 16 18:19 f1.txt.gz -rw-r--r--. 1 root root 168M Dec 16 18:19 f2.txt.gz -rw-r--r--. 1 root root 166M Dec 16 18:19 f3.txt.gz -rw-r--r--. 1 root root 154M Dec 16 18:19 f4.txt.gz -rw-r--r--. 1 root root 151M Dec 16 18:20 f5.txt.gz -rw-r--r--. 1 root root 149M Dec 16 18:20 f6.txt.gz -rw-r--r--. 1 root root 148M Dec 16 18:20 f7.txt.gz -rw-r--r--. 1 root root 146M Dec 16 18:20 f8.txt.gz -rw-r--r--. 1 root root 146M Dec 16 18:21 f9.txt.gz -rw-r--r--. 1 root root 768M Dec 16 18:18 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
4>."-c"选项使用案例
[root@node101.yinzhengjie.org.cn ~]# ll -h total 768M -rw-r--r--. 1 root root 768M Dec 16 18:18 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# gzip -c message > message.gz #结果输出至标准输出,保留原文件不改变 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 917M -rw-r--r--. 1 root root 768M Dec 16 18:18 message -rw-r--r--. 1 root root 149M Dec 16 18:45 message.gz [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
5>.查看gzip的帮助信息
[root@node101.yinzhengjie.org.cn ~]# gzip --help 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 With no FILE, or when FILE is -, read standard input. Report bugs to <bug-gzip@gnu.org>. [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# man gzip
三.bzip2/bunzip2/bzcat(功能要比gzip压缩还要强,比如httpd官方网站的很多压缩包都是基于bzip2压缩方式的,缺点就是压缩时间较长)
bzip2 [OPTION]... FILE ...
-k:keep,
保留原文件 -d:
解压缩 -#:1-9,
压缩比,默认为9
bzcat:
不显式解压缩的前提下查看文本文件内容
1>.安装bzip2软件包
[root@node101.yinzhengjie.org.cn ~]# yum -y install bzip2 Loaded plugins: fastestmirror Determining fastest mirrors * base: mirror.bit.edu.cn * extras: mirror.bit.edu.cn * updates: mirrors.tuna.tsinghua.edu.cn base | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/4): base/7/x86_64/group_gz | 165 kB 00:00:00 (2/4): extras/7/x86_64/primary_db | 153 kB 00:00:00 (3/4): base/7/x86_64/primary_db | 6.0 MB 00:00:00 (4/4): updates/7/x86_64/primary_db | 5.8 MB 00:00:11 Resolving Dependencies --> Running transaction check ---> Package bzip2.x86_64 0:1.0.6-13.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved =================================================================================================================================== Package Arch Version Repository Size =================================================================================================================================== Installing: bzip2 x86_64 1.0.6-13.el7 base 52 k Transaction Summary =================================================================================================================================== Install 1 Package Total download size: 52 k Installed size: 82 k Downloading packages: bzip2-1.0.6-13.el7.x86_64.rpm | 52 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : bzip2-1.0.6-13.el7.x86_64 1/1 Verifying : bzip2-1.0.6-13.el7.x86_64 1/1 Installed: bzip2.x86_64 0:1.0.6-13.el7 Complete! [root@node101.yinzhengjie.org.cn ~]#
2>.bzip2对比gzip最好压缩比例
[root@node101.yinzhengjie.org.cn ~]# ll total 786088 -rw-r--r--. 1 root root 804950000 Dec 16 18:18 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cp message f1.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cp message f2.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 2.3G -rw-r--r--. 1 root root 768M Dec 16 18:49 f1.txt -rw-r--r--. 1 root root 768M Dec 16 18:49 f2.txt -rw-r--r--. 1 root root 768M Dec 16 18:18 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# gzip -9 f1.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bzip2 f2.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 937M -rw-r--r--. 1 root root 146M Dec 16 18:49 f1.txt.gz -rw-r--r--. 1 root root 23M Dec 16 18:49 f2.txt.bz2 -rw-r--r--. 1 root root 768M Dec 16 18:18 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
3>."-k"参数使用案例
[root@node101.yinzhengjie.org.cn ~]# ll -h total 768M -rw-r--r--. 1 root root 768M Dec 16 18:18 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bzip2 -k message #压缩时并不会删除源文件 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 791M -rw-r--r--. 1 root root 768M Dec 16 18:18 message -rw-r--r--. 1 root root 23M Dec 16 18:18 message.bz2 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
4>."-d"参数介绍
[root@node101.yinzhengjie.org.cn ~]# ll total 23256 -rw-r--r--. 1 root root 23810529 Dec 16 18:18 message.bz2 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bzip2 -d message.bz2 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 768M -rw-r--r--. 1 root root 768M Dec 16 18:18 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 786088 -rw-r--r--. 1 root root 804950000 Dec 16 18:18 message [root@node101.yinzhengjie.org.cn ~]#
5>.bzcat命令可以查看bzip2压缩后的文件
[root@node101.yinzhengjie.org.cn ~]# ll total 809344 -rw-r--r--. 1 root root 804950000 Dec 16 18:18 message -rw-r--r--. 1 root root 23810529 Dec 16 18:18 message.bz2 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# bzcat message.bz2 #在不解压缩的前提下查看文件内容 Dec 16 18:03:45 node101 kernel: pci 0000:00:01.0: PCI bridge to [bus 01] Dec 16 18:03:45 node101 kernel: pci 0000:00:01.0: bridge window [io 0x6000-0x7fff] Dec 16 18:03:45 node101 kernel: pci 0000:00:01.0: bridge window [mem 0xe2000000-0xedffffff] Dec 16 18:03:45 node101 kernel: pci 0000:00:01.0: bridge window [mem 0xb0000000-0xdfffffff 64bit pref] Dec 16 18:03:45 node101 kernel: pci 0000:00:0a.0: PCI bridge to [bus 02] .......
6>.查看帮助信息
[root@node101.yinzhengjie.org.cn ~]# bzip2 --help bzip2, a block-sorting file compressor. Version 1.0.6, 6-Sept-2010. usage: bzip2 [flags and input files in any order] -h --help print this message -d --decompress force decompression -z --compress force compression -k --keep keep (don't delete) input files -f --force overwrite existing output files -t --test test compressed file integrity -c --stdout output to standard out -q --quiet suppress noncritical error messages -v --verbose be verbose (a 2nd -v gives more) -L --license display software version & license -V --version display software version & license -s --small use less memory (at most 2500k) -1 .. -9 set block size to 100k .. 900k --fast alias for -1 --best alias for -9 If invoked as `bzip2', default action is to compress. as `bunzip2', default action is to decompress. as `bzcat', default action is to decompress to stdout. If no file names are given, bzip2 compresses or decompresses from standard input to standard output. You can combine short flags, so `-v -4' means the same as -v4 or -4v, &c. [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# man bzip2
四.xz/unxz/xzcat(压缩比上面几种压缩比例都强,linux 内核软件包官方使用的就是这种压缩方式)
xz [OPTION]... FILE ...
-k: keep
保留原文件 -d:
解压缩 -num:
1-9,压缩比,默认为6
unxz file.xz 解压缩
xzcat:
不显式解压缩的前提下查看文本文件内容
1>."-k"参数使用案例
[root@node101.yinzhengjie.org.cn ~]# ll -h total 768M -rw-r--r--. 1 root root 768M Dec 16 18:18 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 786088 -rw-r--r--. 1 root root 804950000 Dec 16 18:18 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# xz -k message #压缩时可以保存源文件 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 786220 -rw-r--r--. 1 root root 804950000 Dec 16 18:18 message -rw-r--r--. 1 root root 131092 Dec 16 18:18 message.xz [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 768M -rw-r--r--. 1 root root 768M Dec 16 18:18 message -rw-r--r--. 1 root root 129K Dec 16 18:18 message.xz [root@node101.yinzhengjie.org.cn ~]#
2>."-d"参数使用案例
[root@node101.yinzhengjie.org.cn ~]# ll total 786220 -rw-r--r--. 1 root root 804950000 Dec 16 18:18 message.log -rw-r--r--. 1 root root 131092 Dec 16 18:18 message.xz [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# xz -d message.xz #解压文件 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 1572176 -rw-r--r--. 1 root root 804950000 Dec 16 18:18 message -rw-r--r--. 1 root root 804950000 Dec 16 18:18 message.log [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
3>.压缩比例对比(随着压缩的比例越大,耗费的时间就越长,默认为6)
[root@node101.yinzhengjie.org.cn ~]# ll -h total 768M -rw-r--r--. 1 root root 768M Dec 16 18:18 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cp message f1.txt [root@node101.yinzhengjie.org.cn ~]# cp message f2.txt [root@node101.yinzhengjie.org.cn ~]# cp message f3.txt [root@node101.yinzhengjie.org.cn ~]# cp message f4.txt [root@node101.yinzhengjie.org.cn ~]# cp message f5.txt [root@node101.yinzhengjie.org.cn ~]# cp message f6.txt [root@node101.yinzhengjie.org.cn ~]# cp message f7.txt [root@node101.yinzhengjie.org.cn ~]# cp message f8.txt [root@node101.yinzhengjie.org.cn ~]# cp message f9.txt [root@node101.yinzhengjie.org.cn ~]# cp message f10.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 7.5G -rw-r--r--. 1 root root 768M Dec 17 04:14 f10.txt -rw-r--r--. 1 root root 768M Dec 17 04:14 f1.txt -rw-r--r--. 1 root root 768M Dec 17 04:14 f2.txt -rw-r--r--. 1 root root 768M Dec 17 04:14 f3.txt -rw-r--r--. 1 root root 768M Dec 17 04:14 f4.txt -rw-r--r--. 1 root root 768M Dec 17 04:14 f5.txt -rw-r--r--. 1 root root 768M Dec 17 04:14 f6.txt -rw-r--r--. 1 root root 768M Dec 17 04:14 f7.txt -rw-r--r--. 1 root root 768M Dec 17 04:14 f8.txt -rw-r--r--. 1 root root 768M Dec 17 04:14 f9.txt -rw-r--r--. 1 root root 768M Dec 16 18:18 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# xz -1 f1.txt [root@node101.yinzhengjie.org.cn ~]# xz -2 f2.txt [root@node101.yinzhengjie.org.cn ~]# xz -3 f3.txt [root@node101.yinzhengjie.org.cn ~]# xz -4 f4.txt [root@node101.yinzhengjie.org.cn ~]# xz -5 f5.txt [root@node101.yinzhengjie.org.cn ~]# xz -6 f6.txt [root@node101.yinzhengjie.org.cn ~]# xz -7 f7.txt [root@node101.yinzhengjie.org.cn ~]# xz -8 f8.txt [root@node101.yinzhengjie.org.cn ~]# xz -9 f9.txt [root@node101.yinzhengjie.org.cn ~]# xz f10.txt [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 769M -rw-r--r--. 1 root root 129K Dec 17 07:33 f10.txt.xz -rw-r--r--. 1 root root 129K Dec 17 07:32 f1.txt.xz -rw-r--r--. 1 root root 129K Dec 17 07:32 f2.txt.xz -rw-r--r--. 1 root root 129K Dec 17 07:32 f3.txt.xz -rw-r--r--. 1 root root 129K Dec 17 07:33 f4.txt.xz -rw-r--r--. 1 root root 129K Dec 17 07:33 f5.txt.xz -rw-r--r--. 1 root root 129K Dec 17 07:33 f6.txt.xz -rw-r--r--. 1 root root 129K Dec 17 07:33 f7.txt.xz -rw-r--r--. 1 root root 129K Dec 17 07:33 f8.txt.xz -rw-r--r--. 1 root root 129K Dec 17 07:33 f9.txt.xz -rw-r--r--. 1 root root 768M Dec 16 18:18 message [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll #最好以字节显示,不然真看不出差别了 total 787408 -rw-r--r--. 1 root root 131092 Dec 17 07:33 f10.txt.xz -rw-r--r--. 1 root root 132040 Dec 17 07:32 f1.txt.xz -rw-r--r--. 1 root root 131956 Dec 17 07:32 f2.txt.xz -rw-r--r--. 1 root root 131940 Dec 17 07:32 f3.txt.xz -rw-r--r--. 1 root root 131928 Dec 17 07:33 f4.txt.xz -rw-r--r--. 1 root root 131572 Dec 17 07:33 f5.txt.xz -rw-r--r--. 1 root root 131092 Dec 17 07:33 f6.txt.xz -rw-r--r--. 1 root root 131092 Dec 17 07:33 f7.txt.xz -rw-r--r--. 1 root root 131092 Dec 17 07:33 f8.txt.xz -rw-r--r--. 1 root root 131092 Dec 17 07:33 f9.txt.xz -rw-r--r--. 1 root root 804950000 Dec 16 18:18 message [root@node101.yinzhengjie.org.cn ~]#
五.zip/unzip
1>.安装zip/unzip工具
[root@node101.yinzhengjie.org.cn ~]# yum -y install zip unzip Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.huaweicloud.com * extras: mirrors.huaweicloud.com * updates: mirrors.tuna.tsinghua.edu.cn Resolving Dependencies --> Running transaction check ---> Package unzip.x86_64 0:6.0-20.el7 will be installed ---> Package zip.x86_64 0:3.0-11.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================================================= Package Arch Version Repository Size ================================================================================================================================= Installing: unzip x86_64 6.0-20.el7 base 170 k zip x86_64 3.0-11.el7 base 260 k Transaction Summary ================================================================================================================================= Install 2 Packages Total download size: 430 k Installed size: 1.1 M Downloading packages: (1/2): unzip-6.0-20.el7.x86_64.rpm | 170 kB 00:00:00 (2/2): zip-3.0-11.el7.x86_64.rpm | 260 kB 00:00:00 --------------------------------------------------------------------------------------------------------------------------------- Total 2.2 MB/s | 430 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : zip-3.0-11.el7.x86_64 1/2 Installing : unzip-6.0-20.el7.x86_64 2/2 Verifying : unzip-6.0-20.el7.x86_64 1/2 Verifying : zip-3.0-11.el7.x86_64 2/2 Installed: unzip.x86_64 0:6.0-20.el7 zip.x86_64 0:3.0-11.el7 Complete! [root@node101.yinzhengjie.org.cn ~]#
2>.打包并压缩文件文件目录
[root@node101.yinzhengjie.org.cn ~]# ll total 0 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# zip -r ./sysconfig.log /etc/sysconfig/ #将"/etc/sysconfig"目录的所有文件(递归)打包到当前目录下的一个叫"sysconfig.log"的文件。 adding: etc/sysconfig/ (stored 0%) adding: etc/sysconfig/cpupower (deflated 25%) adding: etc/sysconfig/man-db (deflated 30%) adding: etc/sysconfig/grub (deflated 27%) adding: etc/sysconfig/cbq/ (stored 0%) adding: etc/sysconfig/cbq/cbq-0000.example (deflated 9%) adding: etc/sysconfig/cbq/avpkt (stored 0%) adding: etc/sysconfig/wpa_supplicant (deflated 44%) adding: etc/sysconfig/run-parts (stored 0%) adding: etc/sysconfig/network-scripts/ (stored 0%) adding: etc/sysconfig/network-scripts/ifup-isdn (deflated 72%) adding: etc/sysconfig/network-scripts/ifdown-Team (deflated 44%) adding: etc/sysconfig/network-scripts/ifdown-ipv6 (deflated 62%) adding: etc/sysconfig/network-scripts/ifdown-tunnel (deflated 44%) adding: etc/sysconfig/network-scripts/ifup-wireless (deflated 49%) adding: etc/sysconfig/network-scripts/ifcfg-lo (deflated 25%) adding: etc/sysconfig/network-scripts/ifup-plip (deflated 45%) adding: etc/sysconfig/network-scripts/init.ipv6-global (deflated 70%) adding: etc/sysconfig/network-scripts/ifup-ipv6 (deflated 70%) adding: etc/sysconfig/network-scripts/ifup-bnep (deflated 47%) adding: etc/sysconfig/network-scripts/ifdown-isdn (deflated 54%) adding: etc/sysconfig/network-scripts/ifup-ippp (deflated 72%) adding: etc/sysconfig/network-scripts/ifdown-ippp (deflated 54%) adding: etc/sysconfig/network-scripts/ifup-eth (deflated 68%) adding: etc/sysconfig/network-scripts/ifup-routes (deflated 63%) adding: etc/sysconfig/network-scripts/ifup-ppp (deflated 63%) adding: etc/sysconfig/network-scripts/ifup-sit (deflated 62%) adding: etc/sysconfig/network-scripts/ifdown-TeamPort (deflated 45%) adding: etc/sysconfig/network-scripts/ifdown-ppp (deflated 58%) adding: etc/sysconfig/network-scripts/ifdown-bnep (deflated 43%) adding: etc/sysconfig/network-scripts/ifdown-post (deflated 57%) adding: etc/sysconfig/network-scripts/network-functions (deflated 71%) adding: etc/sysconfig/network-scripts/ifup-TeamPort (deflated 50%) adding: etc/sysconfig/network-scripts/ifcfg-eth0 (deflated 22%) adding: etc/sysconfig/network-scripts/ifup-post (deflated 65%) adding: etc/sysconfig/network-scripts/ifup-plusb (deflated 48%) adding: etc/sysconfig/network-scripts/ifdown-eth (deflated 64%) adding: etc/sysconfig/network-scripts/ifup-Team (deflated 45%) adding: etc/sysconfig/network-scripts/network-functions-ipv6 (deflated 79%) adding: etc/sysconfig/network-scripts/ifdown-sit (deflated 50%) adding: etc/sysconfig/network-scripts/ifdown-routes (deflated 50%) adding: etc/sysconfig/network-scripts/ifup (deflated 63%) adding: etc/sysconfig/network-scripts/ifup-tunnel (deflated 51%) adding: etc/sysconfig/network-scripts/ifdown (deflated 59%) adding: etc/sysconfig/network-scripts/ifup-aliases (deflated 66%) adding: etc/sysconfig/console/ (stored 0%) adding: etc/sysconfig/ip6tables-config (deflated 61%) adding: etc/sysconfig/sshd (deflated 41%) adding: etc/sysconfig/ebtables-config (deflated 61%) adding: etc/sysconfig/authconfig (deflated 47%) adding: etc/sysconfig/firewalld (stored 0%) adding: etc/sysconfig/init (deflated 52%) adding: etc/sysconfig/modules/ (stored 0%) adding: etc/sysconfig/rsyslog (deflated 23%) adding: etc/sysconfig/network (stored 0%) adding: etc/sysconfig/netconsole (deflated 50%) adding: etc/sysconfig/anaconda (deflated 2%) adding: etc/sysconfig/irqbalance (deflated 45%) adding: etc/sysconfig/selinux (deflated 50%) adding: etc/sysconfig/chronyd (stored 0%) adding: etc/sysconfig/readonly-root (deflated 50%) adding: etc/sysconfig/iptables-config (deflated 60%) adding: etc/sysconfig/crond (deflated 15%) adding: etc/sysconfig/rdisc (stored 0%) adding: etc/sysconfig/kdump (deflated 50%) adding: etc/sysconfig/kernel (deflated 34%) [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 76 -rw-r--r--. 1 root root 76609 Dec 31 04:38 sysconfig.log [root@node101.yinzhengjie.org.cn ~]#
3>.解包压缩文件
[root@node101.yinzhengjie.org.cn ~]# ll total 76 -rw-r--r--. 1 root root 76609 Dec 31 04:38 sysconfig.log [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# unzip sysconfig.log #解包压缩文件 Archive: sysconfig.log creating: etc/sysconfig/ inflating: etc/sysconfig/cpupower inflating: etc/sysconfig/man-db inflating: etc/sysconfig/grub creating: etc/sysconfig/cbq/ inflating: etc/sysconfig/cbq/cbq-0000.example extracting: etc/sysconfig/cbq/avpkt inflating: etc/sysconfig/wpa_supplicant extracting: etc/sysconfig/run-parts creating: etc/sysconfig/network-scripts/ inflating: etc/sysconfig/network-scripts/ifup-isdn inflating: etc/sysconfig/network-scripts/ifdown-Team inflating: etc/sysconfig/network-scripts/ifdown-ipv6 inflating: etc/sysconfig/network-scripts/ifdown-tunnel inflating: etc/sysconfig/network-scripts/ifup-wireless inflating: etc/sysconfig/network-scripts/ifcfg-lo inflating: etc/sysconfig/network-scripts/ifup-plip inflating: etc/sysconfig/network-scripts/init.ipv6-global inflating: etc/sysconfig/network-scripts/ifup-ipv6 inflating: etc/sysconfig/network-scripts/ifup-bnep inflating: etc/sysconfig/network-scripts/ifdown-isdn inflating: etc/sysconfig/network-scripts/ifup-ippp inflating: etc/sysconfig/network-scripts/ifdown-ippp inflating: etc/sysconfig/network-scripts/ifup-eth inflating: etc/sysconfig/network-scripts/ifup-routes inflating: etc/sysconfig/network-scripts/ifup-ppp inflating: etc/sysconfig/network-scripts/ifup-sit inflating: etc/sysconfig/network-scripts/ifdown-TeamPort inflating: etc/sysconfig/network-scripts/ifdown-ppp inflating: etc/sysconfig/network-scripts/ifdown-bnep inflating: etc/sysconfig/network-scripts/ifdown-post inflating: etc/sysconfig/network-scripts/network-functions inflating: etc/sysconfig/network-scripts/ifup-TeamPort inflating: etc/sysconfig/network-scripts/ifcfg-eth0 inflating: etc/sysconfig/network-scripts/ifup-post inflating: etc/sysconfig/network-scripts/ifup-plusb inflating: etc/sysconfig/network-scripts/ifdown-eth inflating: etc/sysconfig/network-scripts/ifup-Team inflating: etc/sysconfig/network-scripts/network-functions-ipv6 inflating: etc/sysconfig/network-scripts/ifdown-sit inflating: etc/sysconfig/network-scripts/ifdown-routes inflating: etc/sysconfig/network-scripts/ifup inflating: etc/sysconfig/network-scripts/ifup-tunnel inflating: etc/sysconfig/network-scripts/ifdown inflating: etc/sysconfig/network-scripts/ifup-aliases creating: etc/sysconfig/console/ inflating: etc/sysconfig/ip6tables-config inflating: etc/sysconfig/sshd inflating: etc/sysconfig/ebtables-config inflating: etc/sysconfig/authconfig extracting: etc/sysconfig/firewalld inflating: etc/sysconfig/init creating: etc/sysconfig/modules/ inflating: etc/sysconfig/rsyslog extracting: etc/sysconfig/network inflating: etc/sysconfig/netconsole inflating: etc/sysconfig/anaconda inflating: etc/sysconfig/irqbalance inflating: etc/sysconfig/selinux extracting: etc/sysconfig/chronyd inflating: etc/sysconfig/readonly-root inflating: etc/sysconfig/iptables-config inflating: etc/sysconfig/crond extracting: etc/sysconfig/rdisc inflating: etc/sysconfig/kdump inflating: etc/sysconfig/kernel [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 80 drwxr-xr-x. 3 root root 4096 Dec 31 04:42 etc -rw-r--r--. 1 root root 76609 Dec 31 04:38 sysconfig.log [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# du -sh etc 384K etc [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 80K drwxr-xr-x. 3 root root 4.0K Dec 31 04:42 etc -rw-r--r--. 1 root root 75K Dec 31 04:38 sysconfig.log [root@node101.yinzhengjie.org.cn ~]#
4>.将系统日志的内容压缩
[root@node101.yinzhengjie.org.cn ~]# ll /var/log/messages -rw-------. 1 root root 309152 Dec 31 04:34 /var/log/messages [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h /var/log/messages -rw-------. 1 root root 302K Dec 31 04:34 /var/log/messages [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 0 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# cat /var/log/messages | zip messages - #将系统日志的内容进行压缩处理并在当前目录生成一个"messages.zip"的文件 adding: - (deflated 81%) [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 60 -rw-r--r--. 1 root root 59455 Dec 31 04:44 messages.zip [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll -h total 60K -rw-r--r--. 1 root root 59K Dec 31 04:44 messages.zip [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
5>.解包解压缩
[root@node101.yinzhengjie.org.cn ~]# ll total 60 -rw-r--r--. 1 root root 59455 Dec 31 04:44 messages.zip [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# unzip -p messages.zip > messages #解压文件内容并重定向到当前目录到"messages"文件中。 [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# ll total 364 -rw-r--r--. 1 root root 309152 Dec 31 04:47 messages -rw-r--r--. 1 root root 59455 Dec 31 04:44 messages.zip [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/12052022.html,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。