6.22 6.1-6.4

6.1 压缩打包介绍

压缩文件和普通文件的区别:

压缩完成后文件变小;

网络传输时占用的带宽资源变少;

Linux下常见的压缩文件后缀名:

.zip/.gz/.bz2/.xz/.tar.gz/.tar.bz2/.tar.xz

Linux下文件后缀名不决定由什么程序打开该文件,只是纯粹的名称;

但为了方便区分,需要将相应的压缩文件写成约定好的后缀名;

 

6.2 gzip压缩工具

 

[root@hyc-01-01 ~]# find /etc/ -type f -name *.conf -exec cat {} >> 1.txt \;

查找/etc/目录下f类型且文件名为*.conf的文件,对查找到的文件执行cat,并将cat执行的结果追加到文件1.txt

[root@hyc-01-01 ~]# find /etc/ -type f -name *.conf -exec cat {} >> 1.txt \;

[root@hyc-01-01 ~]# du -sh 1.txt

256K         1.txt

[root@hyc-01-01 ~]# find /etc/ -type f -name *.conf -exec cat {} >> 1.txt \;

[root@hyc-01-01 ~]# du -sh 1.txt

512K         1.txt

[root@hyc-01-01 ~]# find /etc/ -type f -name *.conf -exec cat {} >> 1.txt \;

[root@hyc-01-01 ~]# du -sh 1.txt

1.0M        1.txt

[root@hyc-01-01 ~]# find /etc/ -type f -name *.conf -exec cat {} >> 1.txt \;

[root@hyc-01-01 ~]# du -sh 1.txt

1.0M        1.txt

[root@hyc-01-01 ~]# find /etc/ -type f -name *.conf -exec cat {} >> 1.txt \;

[root@hyc-01-01 ~]# du -sh 1.txt

2.0M        1.txt

文件大小增加量不正常:

由以上执行可知,每次执行find后文件大小增加256K,所以从512K1.0M至少需要执行两次find,但上面仅执行了一次find即为1.0M

512K1.0M1.0M2.0M以及1.0M1.0M在文件增大时增加的空间中存在空隙,此时文件被压缩,再被解压则文件大小可能会与压缩前文件大小不同

 

[root@hyc-01-01 ~]# wc -l 1.txt

32524 1.txt 统计文件总行数

 

[root@hyc-01-01 ~]# ls

111  1.txt  222  3.txt  anaconda-ks.cfg.1  hyc2  ls2  test.txt  新建文本文档.txt

[root@hyc-01-01 ~]# gzip 1.txt

[root@hyc-01-01 ~]# ls

111       222    anaconda-ks.cfg.1  ls2       新建文本文档.txt

1.txt.gz  3.txt  hyc2 压缩后目录中的1.txt消失,出现压缩包1.txt.gz

[root@hyc-01-01 ~]# du -sh 1.txt.gz

320K         1.txt.gz 压缩后文件大小

[root@hyc-01-01 ~]# gzip -d 1.txt.gz 加压压缩包

[root@hyc-01-01 ~]# du -sh 1.txt

1.3M        1.txt 解压前文件大小2.0M,解压后文件大小1.3M

压缩后再解压缩会将文件的空隙挤压掉,所以解压后的文件比原文件小

[root@hyc-01-01 ~]# wc -l 1.txt

32524 1.txt 文件行数与原先相同

 

指定gzip压缩级别:

[root@hyc-01-01 ~]# gzip -1 1.txt 指定压缩时的级别为1

[root@hyc-01-01 ~]# du -sh 1.txt.gz

380K         1.txt.gz

默认gzip执行6级别的压缩,级别越高压缩的越小,压缩耗费的CPU资源越多

[root@hyc-01-01 ~]# gunzip 1.txt.gz 解压缩,作用与-d相同

[root@hyc-01-01 ~]# gzip -9 1.txt

[root@hyc-01-01 ~]# du -sh 1.txt.gz

320K         1.txt.gz

执行到一定级别后,级别再高,压缩文件也不会变的更小

[root@hyc-01-01 ~]# file 1.txt.gz 显示一个文件的压缩信息

1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Sat Jun 23 21:52:17 2018, max compression 提供的信息包括压缩使用的工具、被压缩文件的名称、文件最后修改时间、使用的压缩级别(max compression最高级)

[root@hyc-01-01 ~]# zcat 1.txt.gz

先解压压缩文件再查看文件内容,查看压缩文件时使用zcat,此时cat无效

 

[root@hyc-01-01 ~]# gunzip -c 1.txt.gz > /tmp/2.txt.t

-c 指定解压缩包时原压缩包不消失

> 指定解压缩后的文件保存的路径及文件名(可以不指定文件名则使用默认)

 

gzip不能压缩目录

 

6.3 bzip2压缩工具

同样不能压缩目录;

一般比gzip压缩的更小;

 

[root@hyc-01-01 ~]# yum install -y bzip2 安装bzip2

[root@hyc-01-01 ~]# bzip2 1.txt 压缩1.txt

[root@hyc-01-01 ~]# du -sh

248K         . 一般压缩的比gzip更小,gzip压缩为320K

[root@hyc-01-01 ~]# bzip2 -d 1.txt.bz2 bzip2解压缩

[root@hyc-01-01 ~]# bunzip2 1.txt.bz2

[root@hyc-01-01 ~]# bzip2 -c 1.txt > /tmp/3.txt.bz2 指定压缩文件路径、名称且保留原文件

[root@hyc-01-01 ~]# du -sh /tmp/3.txt.bz2

132K         /tmp/3.txt.bz2

[root@hyc-01-01 tmp]# bzip2 -d -c 3.txt.bz2 > /root/4.txt 指定解压的路径、文件名称且保留解压包

[root@hyc-01-01 tmp]# cd

[root@hyc-01-01 ~]# du -sh 4.txt

1.3M        4.txt

bzip2的压缩级别:

[root@hyc-01-01 ~]# bzip2 4.txt  

[root@hyc-01-01 ~]# du -sh 4.txt.bz2

132K         4.txt.bz2

[root@hyc-01-01 ~]# bunzip2 4.txt.bz2

[root@hyc-01-01 ~]# bzip2 -9 4.txt

[root@hyc-01-01 ~]# du -sh 4.txt.bz2

132K         4.txt.bz2

bzip2默认压缩级别即为9

[root@hyc-01-01 ~]# cat 4.txt 发现文件无法正常查看

[root@hyc-01-01 ~]# file 4.txt 查看后发现文件类型为bz2压缩包

4.txt: bzip2 compressed data, block size = 900k

[root@hyc-01-01 ~]# file 1.txt 显示文件为普通文本,可以cat

1.txt: UTF-8 Unicode text, with very long lines

查看压缩包的文件内容:

[root@hyc-01-01 ~]# bzcat 4.txt.bz2

6.4 xz压缩工具

[root@hyc-01-01 ~]# xz 1.txt

[root@hyc-01-01 ~]# du -sh 1.txt.xz

52K  1.txt.xz

[root@hyc-01-01 ~]# xz -d 1.txt.xz 解压缩

[root@hyc-01-01 ~]# xz 1.txt

[root@hyc-01-01 ~]# unxz 1.txt.xz 解压缩

[root@hyc-01-01 ~]# xz -c 1.txt > /tmp/1.txt.xz 压缩并保留原文件,将压缩包放到指定位置

[root@hyc-01-01 ~]# ls /tmp/1.txt.xz

/tmp/1.txt.xz

[root@hyc-01-01 ~]# ls 1.txt

1.txt

[root@hyc-01-01 ~]# xz -d -c /tmp/1.txt.xz > ./4.txt 解压到指定位置并保留原文件

[root@hyc-01-01 ~]# ls 4.txt

4.txt

[root@hyc-01-01 ~]# xzcat /tmp/1.txt.xz 查看xz压缩包内容

[root@hyc-01-01 ~]# xz 1.txt

[root@hyc-01-01 ~]# du -sh 1.txt.xz

52K  1.txt.xz

[root@hyc-01-01 ~]# unxz 1.txt.xz

[root@hyc-01-01 ~]# xz -9 1.txt

[root@hyc-01-01 ~]# du -sh 1.txt.xz

52K  1.txt.xz

xz默认使用9级最高级压缩级别




猜你喜欢

转载自blog.51cto.com/12216458/2132143