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 ~]#