使用压缩工具的好处:
使用压缩文件,不只能够节省磁盘空间,并且在传输时还能节省网络宽带。网络
咱们一般讲的家用宽带和机房宽带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