本文共 5209 字,大约阅读时间需要 17 分钟。
压缩的好处是节省磁盘空间,文件变小,网络传输时间变短,带宽变宽,为企业节省大量的资源。在windows中,压缩文件一般有.rar .zip .7z。而在linux中,有.zip、.zg、.bz2、.xz、.tar.gz、.tar.bz2、.tar.xz。我们以后在压缩的时候,名字不要太过个性,应该顺应规则,方便自己,也方便大家。1.gzip压缩工具,他后面直接跟文件名就可以压缩 首先,我们先创建一个目录d6z,然后在里面创建一个文件,命名为1.txt [root@localhost d6z]# ll -rw-r--r--. 1 root root 215614 2月 6 22:33 1.txt 大小为4.2M [root@localhost d6z]# du -sh 1.txt 4.2M 1.txt
然后我们开始压缩
[root@localhost d6z]# gzip 1.txt然后我们查询可以发现,1.txt消失了,取而代之的是1.txt.gz[root@localhost d6z]# ll-rw-r--r--. 1 root root 647491 2月 6 22:36 1.txt.gz大小为636kb[root@localhost d6z]# du -sh 1.txt.gz636K 1.txt.gz然后我们开始解压缩,解压缩的参数是-d,还有一个命令是gunzip
[root@localhost d6z]# gzip -d 1.txt.gz然后我们查询一下[root@localhost d6z]# ll-rw-r--r--. 1 root root 2587368 2月 6 22:36 1.txt[root@localhost d6z]# du -sh 1.txt2.5M 1.txt[root@localhost d6z]# gunzip 1.txt.gz
[root@localhost d6z]# ll总用量 2528-rw-r--r--. 1 root root 2587368 2月 6 22:36 1.txt我们可以发现,解压完后大大小为2.5M,这是因为在压缩时他有一些虚的空间,在解压时就自动的过滤掉了,这个不会影响我们使用。
gzip后面也可以接参数为数字,范围从1~9,这是压缩级别,数值越小,压缩后的大小就越大,耗费的cup资源也就越小。反之数值越大,压缩的最为彻底,大小就越小,耗费的cup资源也就越多,默认值为6,我们只要保持默认就好。
[root@localhost d6z]# du -sh 1.txt.gz(默认压缩值)
636K 1.txt.gz[root@localhost d6z]# gzip -1 1.txt [root@localhost d6z]# du -sh 1.txt.gz(压缩值调整为1)752K 1.txt.gz通过上面的两个命令结果来看,压缩值的不同,压缩后的大小也就不同。我们还可以通过file这个命令来查看压缩文件的详情
[root@localhost d6z]# file 1.txt.gz1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Tue Feb 6 22:36:52 2018, max speed他的含义是:1.txt.gz压缩数据,是来自unix,上一次的更改时间是 2月6日星期二22:36:52 2018,最大速度压缩。还有一个命令可以查看压缩的内容,zcat 1.txt.gz,他实际上是先解压,然后查看。
我们还可以将压缩文件指定到一个目录中,并且不让源文件消失。正常情况下,在压缩完后,源文件都会消失,而变成源文件名+txt.gz这个文件。而且我们在解压缩完后,.gz这个文件也会消失,这样有可能会给我们工作中带来麻烦,如果我们不想让它消失而生成一个新的文件,这时我们可以加参数-c。[root@localhost d6z]# gzip -c 1.txt > /tmp/1.txt.gz然后我们查看一下源文件是否消失
[root@localhost d6z]# ll
-rw-r--r--. 1 root root 2587368 2月 6 22:36 1.txt这是我们发现,源文件并未消失[root@localhost d6z]# ll /tmp/1.txt.gz-rw-r--r--. 1 root root 647491 2月 6 23:26 /tmp/1.txt.gz而在tmp下多出了一个文件,1.txt.gz,这就是我们刚才压缩完成的文件。压缩可以实现,那么解压缩也一样可以实现:
[root@localhost d6z]# gzip -dc /tmp/1.txt.gz > /tmp/d6z/2.txt(d为解压缩参数,c为产生新文件的参数),然后将解压的文件命名为2.txt,它是就是1.txt文件。然后我们查询一下[root@localhost d6z]# cd /tmp[root@localhost tmp]# ll-rw-r--r--. 1 root root 647491 2月 6 23:26 1.txt.gz(原来的压缩文件文件还在)然后我们查询一下[root@localhost d6z]# ll-rw-r--r--. 1 root root 2587368 2月 6 22:36 1.txt-rw-r--r--. 1 root root 2587368 2月 6 23:31 2.txt(成功的添加了2.txt这个文件)然后我们做一下对比[root@localhost d6z]# wc -l 1.txt 2.txt64752 1.txt64752 2.txt发现两个文件行数是一样的。[root@localhost d6z]# du -sh 1.txt 2.txt2.5M 1.txt2.5M 2.txt两个文件大小也是一样的。需要注意的是,gzip不能压缩目录。2.bzip2压缩工具
它和gzip是一样的,但是比gzip压缩的更狠一点,这意味着,它在压缩的时候耗费的cpu资源更多。下面我们可以比较bzip2和gzip压缩同样文件后的比较-rw-r--r--. 1 root root 2587368 2月 6 22:36 1.txt[root@localhost d6z]# du -sh 1.txt2.5M 1.txt我们可以看到,源文件的大小为2.5M先用bzip2来压缩这个文件,然后查询它的文件大小。[root@localhost d6z]# bzip2 1.txt[root@localhost d6z]# ll-rw-r--r--. 1 root root 221774 2月 6 22:36 1.txt.bz2[root@localhost d6z]# du -sh 1.txt.bz2220K 1.txt.bz2用bizp2压缩完后,它的大小为220k然后我们再用gzip来压缩这个文件,然后查询它的文件大小[root@localhost d6z]# gzip 1.txt[root@localhost d6z]# ll-rw-r--r--. 1 root root 647491 2月 6 22:36 1.txt.gz[root@localhost d6z]# du -sh 1.txt.gz636K 1.txt.gz可以发现,同样的文件,用bzip2压缩后的大小为220k,而用gzip压缩后的文件大小为636k,所以bzip2的压缩更为彻底,它和gzip的相同点在于压缩后,源文件都会消失,取而代之的是压缩后的文件。解压缩的参数和gzip是一样的,都为-d。[root@localhost d6z]# bzip2 1.txt[root@localhost d6z]# ll-rw-r--r--. 1 root root 221774 2月 6 22:36 1.txt.bz2然后我们解压缩[root@localhost d6z]# bzip2 -d 1.txt.bz2[root@localhost d6z]# ll-rw-r--r--. 1 root root 2587368 2月 6 22:36 1.txt它也可以通过参数-c指定到特定的路径下
[root@localhost d6z]# bzip2 -c 1.txt > /tmp/3.txt.bz2[root@localhost tmp]# ll-rw-r--r--. 1 root root 647491 2月 6 23:26 1.txt.gz-rw-r--r--. 1 root root 221774 2月 7 22:42 3.txt.bz2这样就可以保留源文件,然后将压缩文件保存到指定的路径下。如果有人不小心,将bz2的压缩文件的后缀名给改掉了,那么我们就需要一个命令去查询一下这个命令的出处,比如我们将1.txt.bz2改成2.txt。
然后我们用less来查看2.txt这个文件。[root@localhost d6z]# less 2.txt然后下面会出现一下提示"2.txt" may be a binary file. See it anyway?2.txt”可能是一个二进制文件。无论如何看到它?这时我们就需要file这个命令来查看这个文件的出处了[root@localhost d6z]# file 2.txt2.txt: bzip2 compressed data, block size = 900k2.txt:bzip2压缩数据,块大小= 900k他提示2.txt是一个bzip2文件,块大小为900。然后我们将它的后缀名改回来即可我们也可以用bzcat来查看bzip2压缩文件中的内容,比如我们只查看1.txt.bz2中的前5行
[root@localhost d6z]# bzcat 1.txt.bz2 | head -5search DHCP HOST
nameserver 202.96.69.38nameserver 202.96.64.68这样就可以实现。
3.xz压缩工具
它与前来那个中压缩工具基本相同。[root@localhost d6z]# xz 1.txt[root@localhost d6z]# ll-rw-r--r--. 1 root root 49104 2月 6 22:36 1.txt.xz[root@localhost d6z]# du -sh 1.txt.xz48K 1.txt.xz可以看出,它的压缩程度是最大的,比前两种都要大。它的解压缩参数也是-d[root@localhost d6z]# xz -d 1.txt.xz[root@localhost d6z]# ll-rw-r--r--. 1 root root 2587368 2月 6 22:36 1.txt也可以接参数-c,指定路径储存压缩后的文件。
[root@localhost d6z]# xz -c 1.txt > /tmp/4.txt.xz[root@localhost tmp]# ll-rw-r--r--. 1 root root 49104 2月 7 23:14 4.txt.xz也可以解压缩到指定文件夹,而保留源文件先将文件解压到指定的文件夹[root@localhost tmp]# xz -dc 4.txt.xz > /tmp/d6z/4.txt然后查看是否解压唱成功[root@localhost d6z]# ll-rw-r--r--. 1 root root 2587368 2月 7 23:19 4.txt通过查看,解压成功。[root@localhost tmp]# ll-rw-r--r--. 1 root root 49104 2月 7 23:14 4.txt.xz而且源文件保存成功。转载于:https://blog.51cto.com/13067688/2074478