首页 > 系统服务 > 详细

Linux操作系统的压缩、解压缩及归档工具介绍

时间:2019-12-17 00:52:06      阅读:116      评论:0      收藏:0      [点我收藏+]

          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 ~]# 
[root@node101.yinzhengjie.org.cn ~]# yum -y install ncompress.x86_64

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 ~]# 
[root@node101.yinzhengjie.org.cn ~]# zcat message.Z                       #预览压缩内容但并不解压

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, dont 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 [OPTION]... FILE ... 
  -d:
    解压缩,相当于gunzip
  -c:
    结果输出至标准输出,保留原文件不改变
  -num:
    这个"num"对应的数字范围是:1-9,指定压缩比,值越大压缩比越大
zcat:
  不显式解压缩的前提下查看文本文件内容

 

 

 

三.

 

四.

 

五.

Linux操作系统的压缩、解压缩及归档工具介绍

原文:https://www.cnblogs.com/yinzhengjie/p/12052022.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!