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 :4.2.4.4-.el7 will be installed
--> Finished Dependency Resolution Dependencies Resolved ===================================================================================================================================
Package Arch Version Repository Size
===================================================================================================================================
Installing:
ncompress x86_64 4.2.4.4-.el7 base k Transaction Summary
===================================================================================================================================
Install Package Total download size: k
Installed size: k
Downloading packages:
ncompress-4.2.4.4-.el7.x86_64.rpm | kB ::
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ncompress-4.2.4.4-.el7.x86_64 /
Verifying : ncompress-4.2.4.4-.el7.x86_64 / Installed:
ncompress.x86_64 :4.2.4.4-.el7 Complete!
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# yum -y install ncompress.x86_64

2>.压缩文件

[root@node101.yinzhengjie.org.cn ~]# ll /var/log/messages -h
-rw------- root root 89K Dec : /var/log/messages
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# for _ in `seq `;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-- root root 87M Dec : 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-- root root 18M Dec : 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-- root root 18M Dec : 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-- root root 87M Dec : 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-- root root 87M Dec : 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-- root root 87M Dec : message
-rw-r--r-- root root 18M Dec : message.Z
[root@node101.yinzhengjie.org.cn ~]#

5>.zcat使用案例

[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r-- root root Dec : message
-rw-r--r-- root root Dec : message.Z
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# zcat message.Z                       #预览压缩内容但并不解压
......
Dec :: node101 systemd: Started Session of user root.
Dec :: node101 systemd: Removed slice User Slice of root.
Dec :: node101 systemd: Starting Cleanup of Temporary Directories...
Dec :: node101 systemd: Started Cleanup of Temporary Directories.
Dec :: node101 systemd: Created slice User Slice of root.
Dec :: node101 systemd: Started Session of user root.
Dec :: node101 systemd: Removed slice User Slice of root.
Dec :: node101 sshd[]: Accepted password for root from 172.30.1.2 port ssh2
Dec :: node101 systemd: Created slice User Slice of root.
Dec :: node101 systemd-logind: New session of user root.
Dec :: node101 systemd: Started Session of user root.
Dec :: node101 chronyd[]: Source 116.203.151.74 replaced with 119.28.183.184
Dec :: node101 chronyd[]: Selected source 119.28.183.184
Dec :: node101 chronyd[]: System clock wrong by 165882.562596 seconds, adjustment started
Dec :: node101 systemd: Started Session of user root.
Dec :: node101 chronyd[]: Source 144.76.76.107 replaced with 193.182.111.141
Dec :: node101 sshd[]: Accepted password for root from 172.30.1.2 port ssh2
Dec :: node101 systemd-logind: New session of user root.
Dec :: node101 systemd: Started Session of user root.
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r-- root root Dec : message
-rw-r--r-- root root Dec : message.Z
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# zcat message.Z                       #预览压缩内容但并不解压

6>.compress也支持交互式压缩

[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r-- root root Dec : message
-rw-r--r-- root root Dec : message.Z
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# compress > test.Z          #按住"ctrl+d"就会结束命令行输出
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r-- root root Dec : message
-rw-r--r-- root root Dec : message.Z
-rw-r--r-- root root Dec : test.Z
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# zcat test.Z | wc -c          #重复的次数越多,压缩比例就越高 [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r-- root root Dec : message
-rw-r--r-- root root Dec : message.Z
-rw-r--r-- root root Dec : test.Z
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

7>."-d"参数介绍

[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r-- root root Dec : message
-rw-r--r-- root root Dec : message.Z
-rw-r--r-- root root Dec : 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
-rw-r--r-- root root Dec : message
-rw-r--r-- root root Dec : message.Z
-rw-r--r-- root root Dec : 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
-rw-r--r-- root root Dec : 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
-rw-r--r-- root root Dec : 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 ~]# compress --help

[root@node101.yinzhengjie.org.cn ~]# man compress 

二.gzip/gunzip(gzip完胜compress)

gzip [OPTION]... FILE ... 
  -d:
    解压缩,相当于gunzip
  -c:
    结果输出至标准输出,保留原文件不改变
  -num:
    这个"num"对应的数字范围是:1-,指定压缩比,值越大压缩比越大,默认压缩比例是6,可参考下面的实验结果。

zcat:
  不显式解压缩的前提下查看文本文件内容

1>.压缩同样文件对比compress和gzip的压缩比例

[root@node101.yinzhengjie.org.cn ~]# for _ in `seq `;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-- root root 877M Dec : 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 .6G
-rw-r--r-- root root 877M Dec : f1.txt
-rw-r--r-- root root 877M Dec : f2.txt
-rw-r--r-- root root 877M Dec : 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 .2G
-rw-r--r-- root root 171M Dec : f1.txt.Z
-rw-r--r-- root root 164M Dec : f2.txt.gz
-rw-r--r-- root root 877M Dec : message
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

2>.解压文件

[root@node101.yinzhengjie.org.cn ~]# ll -h
total .2G
-rw-r--r-- root root 171M Dec : f1.txt.Z
-rw-r--r-- root root 164M Dec : f2.txt.gz
-rw-r--r-- root root 877M Dec : 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 .9G
-rw-r--r-- root root 171M Dec : f1.txt.Z
-rw-r--r-- root root 877M Dec : f2.txt
-rw-r--r-- root root 877M Dec : 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--. root root 768M Dec : 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 .3G
-rw-r--r--. root root 768M Dec : f10.txt
-rw-r--r--. root root 768M Dec : f1.txt
-rw-r--r--. root root 768M Dec : f2.txt
-rw-r--r--. root root 768M Dec : f3.txt
-rw-r--r--. root root 768M Dec : f4.txt
-rw-r--r--. root root 768M Dec : f5.txt
-rw-r--r--. root root 768M Dec : f6.txt
-rw-r--r--. root root 768M Dec : f7.txt
-rw-r--r--. root root 768M Dec : f8.txt
-rw-r--r--. root root 768M Dec : f9.txt
-rw-r--r--. root root 768M Dec : message
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# gzip - f1.txt
[root@node101.yinzhengjie.org.cn ~]# gzip - f2.txt
[root@node101.yinzhengjie.org.cn ~]# gzip - f3.txt
[root@node101.yinzhengjie.org.cn ~]# gzip - f4.txt
[root@node101.yinzhengjie.org.cn ~]# gzip - f5.txt
[root@node101.yinzhengjie.org.cn ~]# gzip - f6.txt
[root@node101.yinzhengjie.org.cn ~]# gzip - f7.txt
[root@node101.yinzhengjie.org.cn ~]# gzip - f8.txt
[root@node101.yinzhengjie.org.cn ~]# gzip - 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 .3G
-rw-r--r--. root root 149M Dec : f10.txt.gz
-rw-r--r--. root root 173M Dec : f1.txt.gz
-rw-r--r--. root root 168M Dec : f2.txt.gz
-rw-r--r--. root root 166M Dec : f3.txt.gz
-rw-r--r--. root root 154M Dec : f4.txt.gz
-rw-r--r--. root root 151M Dec : f5.txt.gz
-rw-r--r--. root root 149M Dec : f6.txt.gz
-rw-r--r--. root root 148M Dec : f7.txt.gz
-rw-r--r--. root root 146M Dec : f8.txt.gz
-rw-r--r--. root root 146M Dec : f9.txt.gz
-rw-r--r--. root root 768M Dec : 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--. root root 768M Dec : 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--. root root 768M Dec : message
-rw-r--r--. root root 149M Dec : 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
-, --fast compress faster
-, --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 ~]# gzip --help

[root@node101.yinzhengjie.org.cn ~]# man gzip

三.bzip2/bunzip2/bzcat(功能要比gzip压缩还要强,比如httpd官方网站的很多压缩包都是基于bzip2压缩方式的,缺点就是压缩时间较长) 

bzip2 [OPTION]... FILE ... 
  -k:keep,
    保留原文件
  -d:
    解压缩
  -#:-,
    压缩比,默认为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 ::
extras | 2.9 kB ::
updates | 2.9 kB ::
(/): base//x86_64/group_gz | kB ::
(/): extras//x86_64/primary_db | kB ::
(/): base//x86_64/primary_db | 6.0 MB ::
(/): updates//x86_64/primary_db | 5.8 MB ::
Resolving Dependencies
--> Running transaction check
---> Package bzip2.x86_64 :1.0.-.el7 will be installed
--> Finished Dependency Resolution Dependencies Resolved ===================================================================================================================================
Package Arch Version Repository Size
===================================================================================================================================
Installing:
bzip2 x86_64 1.0.-.el7 base k Transaction Summary
===================================================================================================================================
Install Package Total download size: k
Installed size: k
Downloading packages:
bzip2-1.0.-.el7.x86_64.rpm | kB ::
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : bzip2-1.0.-.el7.x86_64 /
Verifying : bzip2-1.0.-.el7.x86_64 / Installed:
bzip2.x86_64 :1.0.-.el7 Complete!
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# yum -y install bzip2

2>.bzip2对比gzip最好压缩比例

[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r--. root root Dec : 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 .3G
-rw-r--r--. root root 768M Dec : f1.txt
-rw-r--r--. root root 768M Dec : f2.txt
-rw-r--r--. root root 768M Dec : message
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# gzip - 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--. root root 146M Dec : f1.txt.gz
-rw-r--r--. root root 23M Dec : f2.txt.bz2
-rw-r--r--. root root 768M Dec : 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--. root root 768M Dec : 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--. root root 768M Dec : message
-rw-r--r--. root root 23M Dec : message.bz2
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

4>."-d"参数介绍

[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r--. root root Dec : 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--. root root 768M Dec : message
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r--. root root Dec : message
[root@node101.yinzhengjie.org.cn ~]# 

5>.bzcat命令可以查看bzip2压缩后的文件

[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r--. root root Dec : message
-rw-r--r--. root root Dec : message.bz2
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# bzcat message.bz2       #在不解压缩的前提下查看文件内容
Dec :: node101 kernel: pci ::01.0: PCI bridge to [bus ]
Dec :: node101 kernel: pci ::01.0: bridge window [io 0x6000-0x7fff]
Dec :: node101 kernel: pci ::01.0: bridge window [mem 0xe2000000-0xedffffff]
Dec :: node101 kernel: pci ::01.0: bridge window [mem 0xb0000000-0xdfffffff 64bit pref]
Dec :: node101 kernel: pci ::0a.: PCI bridge to [bus ]
.......

6>.查看帮助信息

[root@node101.yinzhengjie.org.cn ~]# bzip2 --help
bzip2, a block-sorting file compressor. Version 1.0., -Sept-. 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)
- .. - set block size to 100k .. 900k
--fast alias for -
--best alias for - 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 -' means the same as -v4 or -4v, &c. [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# bzip2 --help

[root@node101.yinzhengjie.org.cn ~]# man bzip2 

四.xz/unxz/xzcat(压缩比上面几种压缩比例都强,linux 内核软件包官方使用的就是这种压缩方式)

xz [OPTION]... FILE ... 
  -k: keep
    保留原文件
  -d:
    解压缩
  -num:
    -,压缩比,默认为6 unxz file.xz 解压缩

xzcat:
  不显式解压缩的前提下查看文本文件内容

1>."-k"参数使用案例

[root@node101.yinzhengjie.org.cn ~]# ll -h
total 768M
-rw-r--r--. root root 768M Dec : message
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r--. root root Dec : 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
-rw-r--r--. root root Dec : message
-rw-r--r--. root root Dec : message.xz
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 768M
-rw-r--r--. root root 768M Dec : message
-rw-r--r--. root root 129K Dec : message.xz
[root@node101.yinzhengjie.org.cn ~]#

2>."-d"参数使用案例

[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r--. root root Dec : message.log
-rw-r--r--. root root Dec : 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
-rw-r--r--. root root Dec : message
-rw-r--r--. root root Dec : 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--. root root 768M Dec : 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 .5G
-rw-r--r--. root root 768M Dec : f10.txt
-rw-r--r--. root root 768M Dec : f1.txt
-rw-r--r--. root root 768M Dec : f2.txt
-rw-r--r--. root root 768M Dec : f3.txt
-rw-r--r--. root root 768M Dec : f4.txt
-rw-r--r--. root root 768M Dec : f5.txt
-rw-r--r--. root root 768M Dec : f6.txt
-rw-r--r--. root root 768M Dec : f7.txt
-rw-r--r--. root root 768M Dec : f8.txt
-rw-r--r--. root root 768M Dec : f9.txt
-rw-r--r--. root root 768M Dec : message
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# xz - f1.txt
[root@node101.yinzhengjie.org.cn ~]# xz - f2.txt
[root@node101.yinzhengjie.org.cn ~]# xz - f3.txt
[root@node101.yinzhengjie.org.cn ~]# xz - f4.txt
[root@node101.yinzhengjie.org.cn ~]# xz - f5.txt
[root@node101.yinzhengjie.org.cn ~]# xz - f6.txt
[root@node101.yinzhengjie.org.cn ~]# xz - f7.txt
[root@node101.yinzhengjie.org.cn ~]# xz - f8.txt
[root@node101.yinzhengjie.org.cn ~]# xz - 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--. root root 129K Dec : f10.txt.xz
-rw-r--r--. root root 129K Dec : f1.txt.xz
-rw-r--r--. root root 129K Dec : f2.txt.xz
-rw-r--r--. root root 129K Dec : f3.txt.xz
-rw-r--r--. root root 129K Dec : f4.txt.xz
-rw-r--r--. root root 129K Dec : f5.txt.xz
-rw-r--r--. root root 129K Dec : f6.txt.xz
-rw-r--r--. root root 129K Dec : f7.txt.xz
-rw-r--r--. root root 129K Dec : f8.txt.xz
-rw-r--r--. root root 129K Dec : f9.txt.xz
-rw-r--r--. root root 768M Dec : message
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll          #最好以字节显示,不然真看不出差别了
total
-rw-r--r--. root root Dec : f10.txt.xz
-rw-r--r--. root root Dec : f1.txt.xz
-rw-r--r--. root root Dec : f2.txt.xz
-rw-r--r--. root root Dec : f3.txt.xz
-rw-r--r--. root root Dec : f4.txt.xz
-rw-r--r--. root root Dec : f5.txt.xz
-rw-r--r--. root root Dec : f6.txt.xz
-rw-r--r--. root root Dec : f7.txt.xz
-rw-r--r--. root root Dec : f8.txt.xz
-rw-r--r--. root root Dec : f9.txt.xz
-rw-r--r--. root root Dec : 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 :6.0-.el7 will be installed
---> Package zip.x86_64 :3.0-.el7 will be installed
--> Finished Dependency Resolution Dependencies Resolved =================================================================================================================================
Package Arch Version Repository Size
=================================================================================================================================
Installing:
unzip x86_64 6.0-.el7 base k
zip x86_64 3.0-.el7 base k Transaction Summary
=================================================================================================================================
Install Packages Total download size: k
Installed size: 1.1 M
Downloading packages:
(/): unzip-6.0-.el7.x86_64.rpm | kB ::
(/): zip-3.0-.el7.x86_64.rpm | kB ::
---------------------------------------------------------------------------------------------------------------------------------
Total 2.2 MB/s | kB ::
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : zip-3.0-.el7.x86_64 /
Installing : unzip-6.0-.el7.x86_64 /
Verifying : unzip-6.0-.el7.x86_64 /
Verifying : zip-3.0-.el7.x86_64 / Installed:
unzip.x86_64 :6.0-.el7 zip.x86_64 :3.0-.el7 Complete!
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# yum -y install zip unzip

2>.打包并压缩文件文件目录

[root@node101.yinzhengjie.org.cn ~]# ll
total
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# zip -r ./sysconfig.log /etc/sysconfig/            #将"/etc/sysconfig"目录的所有文件(递归)打包到当前目录下的一个叫"sysconfig.log"的文件。
adding: etc/sysconfig/ (stored %)
adding: etc/sysconfig/cpupower (deflated %)
adding: etc/sysconfig/man-db (deflated %)
adding: etc/sysconfig/grub (deflated %)
adding: etc/sysconfig/cbq/ (stored %)
adding: etc/sysconfig/cbq/cbq-.example (deflated %)
adding: etc/sysconfig/cbq/avpkt (stored %)
adding: etc/sysconfig/wpa_supplicant (deflated %)
adding: etc/sysconfig/run-parts (stored %)
adding: etc/sysconfig/network-scripts/ (stored %)
adding: etc/sysconfig/network-scripts/ifup-isdn (deflated %)
adding: etc/sysconfig/network-scripts/ifdown-Team (deflated %)
adding: etc/sysconfig/network-scripts/ifdown-ipv6 (deflated %)
adding: etc/sysconfig/network-scripts/ifdown-tunnel (deflated %)
adding: etc/sysconfig/network-scripts/ifup-wireless (deflated %)
adding: etc/sysconfig/network-scripts/ifcfg-lo (deflated %)
adding: etc/sysconfig/network-scripts/ifup-plip (deflated %)
adding: etc/sysconfig/network-scripts/init.ipv6-global (deflated %)
adding: etc/sysconfig/network-scripts/ifup-ipv6 (deflated %)
adding: etc/sysconfig/network-scripts/ifup-bnep (deflated %)
adding: etc/sysconfig/network-scripts/ifdown-isdn (deflated %)
adding: etc/sysconfig/network-scripts/ifup-ippp (deflated %)
adding: etc/sysconfig/network-scripts/ifdown-ippp (deflated %)
adding: etc/sysconfig/network-scripts/ifup-eth (deflated %)
adding: etc/sysconfig/network-scripts/ifup-routes (deflated %)
adding: etc/sysconfig/network-scripts/ifup-ppp (deflated %)
adding: etc/sysconfig/network-scripts/ifup-sit (deflated %)
adding: etc/sysconfig/network-scripts/ifdown-TeamPort (deflated %)
adding: etc/sysconfig/network-scripts/ifdown-ppp (deflated %)
adding: etc/sysconfig/network-scripts/ifdown-bnep (deflated %)
adding: etc/sysconfig/network-scripts/ifdown-post (deflated %)
adding: etc/sysconfig/network-scripts/network-functions (deflated %)
adding: etc/sysconfig/network-scripts/ifup-TeamPort (deflated %)
adding: etc/sysconfig/network-scripts/ifcfg-eth0 (deflated %)
adding: etc/sysconfig/network-scripts/ifup-post (deflated %)
adding: etc/sysconfig/network-scripts/ifup-plusb (deflated %)
adding: etc/sysconfig/network-scripts/ifdown-eth (deflated %)
adding: etc/sysconfig/network-scripts/ifup-Team (deflated %)
adding: etc/sysconfig/network-scripts/network-functions-ipv6 (deflated %)
adding: etc/sysconfig/network-scripts/ifdown-sit (deflated %)
adding: etc/sysconfig/network-scripts/ifdown-routes (deflated %)
adding: etc/sysconfig/network-scripts/ifup (deflated %)
adding: etc/sysconfig/network-scripts/ifup-tunnel (deflated %)
adding: etc/sysconfig/network-scripts/ifdown (deflated %)
adding: etc/sysconfig/network-scripts/ifup-aliases (deflated %)
adding: etc/sysconfig/console/ (stored %)
adding: etc/sysconfig/ip6tables-config (deflated %)
adding: etc/sysconfig/sshd (deflated %)
adding: etc/sysconfig/ebtables-config (deflated %)
adding: etc/sysconfig/authconfig (deflated %)
adding: etc/sysconfig/firewalld (stored %)
adding: etc/sysconfig/init (deflated %)
adding: etc/sysconfig/modules/ (stored %)
adding: etc/sysconfig/rsyslog (deflated %)
adding: etc/sysconfig/network (stored %)
adding: etc/sysconfig/netconsole (deflated %)
adding: etc/sysconfig/anaconda (deflated %)
adding: etc/sysconfig/irqbalance (deflated %)
adding: etc/sysconfig/selinux (deflated %)
adding: etc/sysconfig/chronyd (stored %)
adding: etc/sysconfig/readonly-root (deflated %)
adding: etc/sysconfig/iptables-config (deflated %)
adding: etc/sysconfig/crond (deflated %)
adding: etc/sysconfig/rdisc (stored %)
adding: etc/sysconfig/kdump (deflated %)
adding: etc/sysconfig/kernel (deflated %)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r--. root root Dec : sysconfig.log
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# zip -r ./sysconfig.log /etc/sysconfig/            #将"/etc/sysconfig"目录的所有文件(递归)打包到当前目录下的一个叫"sysconfig.log"的文件。

3>.解包压缩文件

[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r--. root root Dec : 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-.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
drwxr-xr-x. root root Dec : etc
-rw-r--r--. root root Dec : 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. root root .0K Dec : etc
-rw-r--r--. root root 75K Dec : sysconfig.log
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# unzip sysconfig.log                 #解包压缩文件

4>.将系统日志的内容压缩

[root@node101.yinzhengjie.org.cn ~]# ll /var/log/messages
-rw-------. root root Dec : /var/log/messages
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll -h /var/log/messages
-rw-------. root root 302K Dec : /var/log/messages
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll
total
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /var/log/messages | zip messages -      #将系统日志的内容进行压缩处理并在当前目录生成一个"messages.zip"的文件
adding: - (deflated %)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r--. root root Dec : messages.zip
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll -h
total 60K
-rw-r--r--. root root 59K Dec : messages.zip
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# cat /var/log/messages | zip messages -      #将系统日志的内容进行压缩处理并在当前目录生成一个"messages.zip"的文件

5>.解包解压缩

[root@node101.yinzhengjie.org.cn ~]# ll
total
-rw-r--r--. root root Dec : 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
-rw-r--r--. root root Dec : messages
-rw-r--r--. root root Dec : messages.zip
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# unzip -p messages.zip > messages          #解压文件内容并重定向到当前目录到"messages"文件中。

最新文章

  1. Google Authentication的实现 - Odoo 安全登录
  2. MindManager使用说明
  3. linux学习笔记1
  4. Gradle tip #3: Tasks ordering
  5. ADO.NET(查询、属性扩展)
  6. Android 学习(一)
  7. Linux Bash命令关于程序调试详解
  8. 15_CXF和Spring开发手机号查询网站
  9. struts 2 debug标签隐藏不显示
  10. OBIEE接受外部参数
  11. URL中的#
  12. android性能调优之traceview的使用
  13. jQuery children等筛选用法
  14. spring框架整合hibernate框架简单操作数据库
  15. Python的命名空间及作用域
  16. 转:vw适配中使用伪类选择器遇到的问题
  17. 据说是Flord算法
  18. confluence 新tab 页面打开 kibana short link
  19. Balanced Numbers (数位DP)
  20. (转)常见LCD接口

热门文章

  1. 简析平衡树(四)——FHQ Treap
  2. xBIM初步使用
  3. 【转】spring的AOP原理,使用场景是什么?
  4. inputType导致TextView不能多行显示
  5. java 声明并初始化整型变量
  6. Java 性能调优小技巧
  7. ffmpeg Operation not permitted 报错的解决过程记录
  8. 大话设计模式Python实现-代理模式
  9. Python文件读写机制
  10. Loj #3085. 「GXOI / GZOI2019」特技飞行