使用壓縮工具的好處:
使用壓縮文件,不只能夠節省磁盤空間,並且在傳輸時還能節省網絡寬帶。網絡
咱們一般講的家用寬帶和機房寬帶100M是有區別的:
機房寬帶的上行和下行都是100M,因此價格昂貴,家用寬帶下行是100M,可是上行每每只有10M-20M工具
格式:gzip [參數] filename -d是解壓縮。spa
例子:查找/etc/下後綴爲.conf的文件,並將它的內容追加到文件1.txt中,而且壓縮它,而後再解壓。unix
[root@yong-02 gzip]# find /etc/ -type f -name "*conf" -exec cat {} >> 2.txt \; [root@yong-02 gzip]# du -sh 2.txt 4.0M 1.txt //這裏要注意一下,這個大小不太準確,這裏屢次追加會看到文件,du -sh 1.txt查看的文件數值不一樣,但在屢次查看,文件大小會恢復正常。(跳轉數值較大比,是由於這個文件自己存在不少空隙,最後在壓縮並解壓後,會發現大小會有不一樣) [root@yong-02 gzip]# wc -l 2.txt 54738 2.txt [root@yong-02 gzip]# gzip 2.txt [root@yong-02 gzip]# ls 2.txt.gz [root@yong-02 gzip]# gzip -d 2.txt [root@yong-02 gzip]# du -sh 2.txt 4.0M 2.txt [root@yong-02 gzip]# gzip 2.txt [root@yong-02 gzip]# du -sh 2.txt.gz 560K 2.txt.gz [root@yong-02 gzip]# gunzip 2.txt.gz [root@yong-02 gzip]# ls 2.txt [root@yong-02 gzip]# gzip 2.txt [root@yong-02 gzip]# du -sh 2.txt.gz 560K 2.txt.gz
[root@yong-02 gzip]# gzip -c 2.txt > /tmp/2.txt.gz [root@yong-02 gzip]# ls 2.txt [root@yong-02 gzip]# ls /tmp/2.txt.gz /tmp/2.txt.gz [root@yong-02 gzip]# du -sh /tmp/2.txt.gz 560K /tmp/2.txt.gz
[root@yong-02 gzip]# gzip -d -c /tmp/2.txt.gz > ./3.txt [root@yong-02 gzip]# ls 2.txt 3.txt [root@yong-02 gzip]# wc -l 2.txt 3.txt 54738 2.txt 54738 3.txt 109476 總用量 [root@yong-02 gzip]# du -sh 2.txt 3.txt 2.1M 2.txt 2.1M 3.txt
[root@yong-02 gzip]# zcat /tmp/2.txt.gz
[root@yong-02 gzip]# file /tmp/2.txt.gz /tmp/2.txt.gz: gzip compressed data, was "2.txt", from Unix, last modified: Mon Apr 16 14:43:00 2018 /tmp/2.txt.gz 壓縮數據是2.txt,基於unix平臺,最後修改時間是2018年4月16日星期一
bzip命令的格式:bzip2 [-dz] filename ,壓縮文件時加不加-z都同樣,-d 解壓縮。
bzip比gzip壓縮更小,所耗費的CPU資源也最大(壓縮的文件也是最小的)code
第一次使用bzip2命令時提示沒有這個命令,咱們用yum安裝一下ip
[root@yong-02 ~]# yum install -y bzip2
[root@yong-02 gzip]# bzip2 2.txt [root@yong-02 gzip]# ls 2.txt.bz2 3.txt
[root@yong-02 gzip]# bzip2 -d 2.txt.bz2 [root@yong-02 gzip]# ls 2.txt 3.txt
[root@yong-02 gzip]# bzip2 -c 2.txt > /tmp/2.txt.gz [root@yong-02 gzip]# ls /tmp/2.txt.gz /tmp/2.txt.gz [root@yong-02 gzip]# ls 2.txt 3.txt
[root@yong-02 gzip]# bzip2 -d -c /tmp/2.txt.gz > ./4.txt [root@yong-02 gzip]# ls 2.txt 3.txt 4.txt [root@yong-02 gzip]# ls /tmp/2.txt.gz /tmp/2.txt.gz
[root@yong-02 gzip]# bzcat /tmp/2.txt.gz
[root@yong-02 gzip]# file /tmp/2.txt.gz /tmp/2.txt.gz: bzip2 compressed data, block size = 900k //bzip2壓縮數據,大小爲900k
xz命令格式:xz[-zd] filename 壓縮文件加不加-z均可以,-d解壓縮。
xz壓縮文件比bzip2更小,所耗費的CPU資源也最大(壓縮的文件也是最小的)資源
xz 1.txt / xz -z 1.txt //壓縮文件ast
xz -d 1.txt.xz / unxz 1.txt.xz //解壓縮文件sed
xz -# 1.txt //#範圍1-9,默認9打包
不能壓縮目錄
xzcat 1.txt.xz //查看壓縮文件內容
xz -c 1.txt > /root/1.txt.xz //指定壓縮文件路徑,而且源文件存在
xz -d -c /root/1.txt.xz > 1.txt.new3 //解壓文件到指定路徑下,而且源壓縮文件存在 同unxz
file 1.txt.xz查看文件屬性
壓縮文件2.txt
[root@yong-02 gzip]# xz 2.txt [root@yong-02 gzip]# ls 2.txt.xz 3.txt 4.txt
[root@yong-02 gzip]# xz -d 2.txt.xz [root@yong-02 gzip]# ls 2.txt 3.txt 4.txt
[root@yong-02 gzip]# xz -c 2.txt > /tmp/2.txt.xz [root@yong-02 gzip]# ls 2.txt 3.txt 4.txt [root@yong-02 gzip]# ls /tmp/2.txt.xz /tmp/2.txt.xz
[root@yong-02 gzip]# xz -d -c /tmp/2.txt.xz > ./5.txt [root@yong-02 gzip]# ls 2.txt 3.txt 4.txt 5.txt [root@yong-02 gzip]# ls /tmp/2.txt.xz /tmp/2.txt.xz
[root@yong-02 gzip]# file /tmp/2.txt.xz /tmp/2.txt.xz: XZ compressed data //xz壓縮數據。
[root@yong-02 gzip]# du -sh 2.txt /tmp/2.txt.gz /tmp/2.txt.bz2 /tmp/2.txt.xz 2.1M 2.txt 900K /tmp/2.txt.gz 232K /tmp/2.txt.bz2 60K /tmp/2.txt.xz