6.1 压缩打包介绍
window:.rar .zip .7z
Linux : .zip .gz .bz2 .xz .tar.gz .tar.bz2 .tar.xz
压缩文件的作用:方便传输、节省空间、节省传输时间、带宽资源耗费变少。
6.2 gzip压缩工具 --gzip不能压缩目录
gzip 1.txt
[root@wangshuang-01 gg]# find /etc/ -type f -name "*conf" -exec cat {} >>1.txt \; //查找conf结尾的文件追加到1.txt
[root@wangshuang-01 gg]# du -sh 1.txt //查看文件大小
2.0M 1.txt
[root@wangshuang-01 gg]# wc -l 1.txt //行数
48656 1.txt
[root@wangshuang-01 gg]# gzip 1.txt //压缩
[root@wangshuang-01 gg]# ls
1.txt.gz
[root@wangshuang-01 gg]# du -sh 1.txt.gz
500K 1.txt.gz
gzip -d 1.txt.gz / gunzip 1.txt.gz
[root@wangshuang-01 gg]# gzip -d 1.txt.gz //解压
[root@wangshuang-01 gg]# ls
1.txt
[root@wangshuang-01 gg]# wc -l 1.txt
48656 1.txt
[root@wangshuang-01 gg]# gunzip 1.txt.gz
[root@wangshuang-01 gg]# ls
1.txt
gzip -# 1.txt //#范围1-9,默认6 ,代表压缩级别,1,最不严谨(耗费的CPU资源小)
[root@wangshuang-01 gg]# gzip -1 1.txt
[root@wangshuang-01 gg]# du -sh 1.txt.gz
588K 1.txt.gz
[root@wangshuang-01 gg]# gzip -9 1.txt
[root@wangshuang-01 gg]# du -sh 1.txt.gz
496K 1.txt.gz
查看压缩文件
[root@wangshuang-01 gg]# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Mon Jun 18 13:33:18 2018
查看压缩文件的内容,先解压,再访问
zcat 1.txt.gz
压缩时指定文件,保留原文件,
gzip -c 1.txt > /tmp/1.txt.gz
[root@wangshuang-01 gg]# gzip -c 1.txt > /tmp/1.txt.gz
[root@wangshuang-01 gg]# ls
1.txt
[root@wangshuang-01 gg]# ls /tmp/1.txt.gz
/tmp/1.txt.gz
[root@wangshuang-01 gg]# file !$
file /tmp/1.txt.gz
/tmp/1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Mon Jun 18 13:33:18 2018
解压时保留压缩文件,指定解压文件
gunzip -c /tmp/1.txt.gz > /tmp/1.txt.new
[root@wangshuang-01 gg]# gunzip -c /tmp/1.txt.gz > 2.txt
[root@wangshuang-01 gg]# ls
1.txt 2.txt
[root@wangshuang-01 gg]# du -sh 2.txt
1.9M 2.txt
6.3 bzip2压缩工具--不能压缩目录
安装bzip2:yum install -y bzip2
bzip2 1.txt /bzip2 -z 1.txt
[root@wangshuang-01 gg]# bzip2 1.txt
[root@wangshuang-01 gg]# ls
1.txt.bz2 2.txt
[root@wangshuang-01 gg]# du -sh 1.txt.bz2
192K 1.txt.bz2
bzip2 -d 1.txt.bz2 /bunzip 1.txt.bz2
[root@wangshuang-01 gg]# bzip2 -d /tmp/1.txt.bz2
[root@wangshuang-01 gg]# bunzip2 1.txt.bz2
[root@wangshuang-01 gg]# ls
1.txt 2.txt
bzip -# 1.txt //#范围1-9 ,默认9
[root@wangshuang-01 gg]# bzip2 -9 1.txt
[root@wangshuang-01 gg]# ls
1.txt.bz2 2.txt 3.txt
[root@wangshuang-01 gg]# file 1.txt.bz2
1.txt.bz2: bzip2 compressed data, block size = 900k
bzcat 1.txt.bz2
查看文件内容:
[root@wangshuang-01 gg]# bzcat 1.txt.bz2
bzip2 -c 1.txt > /tmp/1.txt.bz2
[root@wangshuang-01 gg]# bzip2 -c 1.txt > /tmp/1.txt.bz2
[root@wangshuang-01 gg]# du -sh /tmp/1.txt.bz2
192K /tmp/1.txt.bz2
bzip2 -c -d /tmp/1.txt.bz2 > /tmp/1.txt.new2
[root@wangshuang-01 gg]# bzip2 -d -c /tmp/1.txt.bz2 > 3.txt
[root@wangshuang-01 gg]# ls
1.txt 2.txt 3.txt
6.4 xz压缩工具 --不能压缩目录
xz 2.txt /xz -z 2.txt
[root@wangshuang-01 gg]# xz 2.txt
[root@wangshuang-01 gg]# ls
1.txt.bz2 2.txt.xz 3.txt
[root@wangshuang-01 gg]# du -sh 2.txt.xz
60K 2.txt.xz
xz -d 2.txt.xz / unxz 2.txt.xz
[root@wangshuang-01 gg]# xz 2.txt
[root@wangshuang-01 gg]# ls
1.txt.bz2 2.txt.xz 3.txt
[root@wangshuang-01 gg]# xz -d 2.txt.xz
[root@wangshuang-01 gg]# ls
1.txt.bz2 2.txt 3.txt
[root@wangshuang-01 gg]# xz 2.txt
[root@wangshuang-01 gg]# ls
1.txt.bz2 2.txt.xz 3.txt
[root@wangshuang-01 gg]# unxz 2.txt.xz
[root@wangshuang-01 gg]# ls
1.txt.bz2 2.txt 3.txt
xz -# 2.txt //范围1-9,默认6
[root@wangshuang-01 gg]# du -sh 2.txt
1.9M 2.txt
[root@wangshuang-01 gg]# xz -9 2.txt
[root@wangshuang-01 gg]# ls
1.txt.bz2 2.txt.xz 3.txt 4.txt
[root@wangshuang-01 gg]# du -sh 2.txt.xz
60K 2.txt.xz
[root@wangshuang-01 gg]# unxz 2.txt.xz
[root@wangshuang-01 gg]# ls
1.txt.bz2 2.txt 3.txt 4.txt
[root@wangshuang-01 gg]# xz -6 2.txt
[root@wangshuang-01 gg]# du -sh 2.txt.xz
60K 2.txt.xz
xzcat 1.txt.xz //查看压缩内容
[root@wangshuang-01 gg]# xzcat /tmp/2.txt.xz
xz -c 2.txt > /tmp/2.txt.xz
[root@wangshuang-01 gg]# xz -c 2.txt > /tmp/2.txt.xz
[root@wangshuang-01 gg]# ls /tmp/2.txt.xz
/tmp/2.txt.xz
xz -d -c /tmp/2.txt.xz > 4.txt.new3
[root@wangshuang-01 gg]# xz -d -c /tmp/2.txt.xz > 4.txt
[root@wangshuang-01 gg]# ls
1.txt.bz2 2.txt 3.txt 4.txt