6.1压缩打包介绍
Windows上经常会用到打包的文件,比如一些后缀.rar,.zip,.7z。
压缩的文件传输起来会增加效率。对于一些大公司的机房带宽很昂贵,压缩的文件上传下载可以节省很多带宽。因此,压缩是一个节省带宽的好方法。
linux下也有很多压缩包,后缀有.zip,.gz,.bz2,.xz,.tar.gz,.tar.bz2,.tar.xz。
虽然linux下的后缀大多是约定的,并不要求,但是压缩包的压缩和解压对后缀名还是有一定的要求的,遵循我们的约定,方便你我。
6.2 gzip压缩工具
gzip是linux下的常见的一种压缩工具,很简单,直接跟目录名就可以了
下面我们做一些准备工作,在/tmp下创建一个d6z(第六章)的目录,然后find查询/etc下的文件cat重定向到1.txt。
[root@localhost ~]# cd /tmp/
[root@localhost tmp]# mkdir d6z/
[root@localhost tmp]# cd !$
cd d6z/
[root@localhost d6z]# ls
[root@localhost d6z]# find /etc/ -type f -name "*.conf" -exec cat {} >> 1.txt \;
[root@localhost d6z]# ls
1.txt
[root@localhost d6z]# ls -h 1.txt
1.txt
[root@localhost d6z]# ls -lh 1.txt
-rw-r--r--. 1 root root 210K Jun 22 15:07 1.txt
[root@localhost d6z]# du -sh 1.txt
212K 1.txt
[root@localhost d6z]# du -sb 1.txt
214469 1.txt
[root@localhost d6z]# cat 1.txt >> 1.txt
cat: 1.txt: input file is output file
[root@localhost d6z]# cat 1.txt >> 2.txt
[root@localhost d6z]# cat 2.txt >> 1.txt
[root@localhost d6z]# cat 2.txt >> 1.txt
[root@localhost d6z]# cat 2.txt >> 1.txt
[root@localhost d6z]# cat 2.txt >> 1.txt
[root@localhost d6z]# cat 2.txt >> 1.txt
[root@localhost d6z]# cat 1.txt >> 2.txt
[root@localhost d6z]# cat 1.txt >> 2.txt
[root@localhost d6z]# cat 1.txt >> 2.txt
[root@localhost d6z]# cat 1.txt >> 2.txt
[root@localhost d6z]# cat 1.txt >> 2.txt
[root@localhost d6z]# cat 1.txt >> 2.txt
[root@localhost d6z]# cat 2.txt >> 1.txt
[root@localhost d6z]# cat 2.txt >> 1.txt
[root@localhost d6z]# wc -l 1.txt
428560 1.txt
[root@localhost d6z]# du -sh 1.txt
33M 1.txt
下面我们就用gzip工具压缩。
[root@localhost d6z]# ls
1.txt 2.txt
[root@localhost d6z]# gzip 1.txt
[root@localhost d6z]# ls
1.txt.gz 2.txt
[root@localhost d6z]# du -sh 1.txt.gz
4.1M 1.txt.gz
我们可以很明显的看到文件从33M压缩到了4.1M。
那么我们下载了一个.gz的文件可以用-d选项解压这个文件。
[root@localhost d6z]# gzip -d 1.txt.gz
[root@localhost d6z]# ls
1.txt 2.txt
[root@localhost d6z]# du -sh 1.txt
17M 1.txt
解压后文本文件变成了17M,原因是我们生成这个文本的时候使用的是重定向符,这样生成的文本会有一些空间是“虚的”,压缩以后这部分就消失了。
[root@localhost d6z]# du -sh 1.txt
17M 1.txt
[root@localhost d6z]# gzip 1.txt
[root@localhost d6z]# du -sh 1.txt.gz
4.1M 1.txt.gz
[root@localhost d6z]# gzip -d 1.txt.gz
[root@localhost d6z]# du -sh 1.txt
17M 1.txt
重新做一遍,这个问题就消失了。
-#选项可以调整gzip的压缩级别,这里的#是数字[1-9],默认级别是6,也可以调整。
-# --fast --best Regulate the speed of compression using the specified digit #, where -1 or --fast indicates the fastest compression method (less compression) and -9 or --best indicates the slowest compression method (best compression). The default compression level is -6 (that is, biased towards high compression at expense of speed).
解压的命令也可以使用gunzip + ×.gz命令
[root@localhost d6z]# gzip -9 1.txt
[root@localhost d6z]# du -sh 1.txt.gz
4.1M 1.txt.gz
[root@localhost d6z]# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Fri Jun 22 15:11:07 2018, max compression
压缩文件的内容使用cat等命令是不能查看内容的,如果想查看可以使用zcat配合管道符|的方法。
[root@localhost d6z]# zcat 1.txt.gz | head
# Generated by NetworkManager
nameserver 8.8.8.8
# The upstream Mozilla.org project tests all changes to the root CA
# list with the NSS (Network Security Services) library.
#
# Occassionally, changes might cause compatibility issues with
# other cryptographic libraries, such as openssl or gnutls.
#
# The package maintainers of the CA certificates package might decide
# to temporarily keep certain (legacy) root CA certificates trusted,
使用-c选项(Write output on standard output)可以生成一个新的压缩文件(不加这个选项会就地压缩)
[root@localhost d6z]# ls
1.txt.gz 2.txt
[root@localhost d6z]# gunzip 1.txt.gz
[root@localhost d6z]# ls
1.txt 2.txt
[root@localhost d6z]# gzip -c 1.txt > 1.txt.gz
[root@localhost d6z]# ls
1.txt 1.txt.gz 2.txt
同样解压也是一个道理
[root@localhost d6z]# ls
1.txt 1.txt.gz
[root@localhost d6z]# gzip -d -c 1.txt.gz > 2.txt
[root@localhost d6z]# ls
1.txt 1.txt.gz 2.txt
[root@localhost d6z]# diff 2.txt 1.txt
[root@localhost d6z]#
最后,gzip不能压缩目录。
[root@localhost tmp]# gzip d6z/
gzip: d6z/ is a directory -- ignored
6.3 bzip2压缩工具
bzip2压缩工具的算法与gzip不同,会消耗更多的资源,压缩比更高。
[root@localhost d6z]# du -sh *
17M 1.txt
4.1M 1.txt.gz
17M 2.txt
[root@localhost d6z]# bzip2 2.txt
[root@localhost d6z]# du -sh *
17M 1.txt
4.1M 1.txt.gz
1.4M 2.txt.bz2
对于相同的文件,使用gzip压缩到了4.1M而bzip2可以压缩到1.4M。
bzip2的选项和gzip差不多。
解压的时候也使用-d选项。
[root@localhost d6z]# bzip2 -d 2.txt.bz2
[root@localhost d6z]# du -sh *
17M 1.txt
4.1M 1.txt.gz
17M 2.txt
也可以使用bunzip2解压。
同样也有标准输出-c选项。
同样也有压缩级别(默认为9)。
同样也不支持压缩目录。
如果这个压缩文件的后缀不是.bz2,解压的时候会重新命名这个文件(也是可以解压的)
[root@localhost d6z]# bzip2 2.txt
ls[root@localhost d6z]# ls
1.txt 1.txt.gz 2.txt.bz2
[root@localhost d6z]# mv 2.txt.bz2 2.txt
[root@localhost d6z]# ls
1.txt 1.txt.gz 2.txt
[root@localhost d6z]# bzip2 -d 2.txt
bzip2: Can't guess original name for 2.txt -- using 2.txt.out
[root@localhost d6z]# ls
1.txt 1.txt.gz 2.txt.out
[root@localhost d6z]# less 2.txt.out
到底是什么样的文件,使用file命令查看。
[root@localhost d6z]# file *
1.txt: C source, UTF-8 Unicode text
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Fri Jun 22 15:11:07 2018
2.txt.out: C source, UTF-8 Unicode text
同样,查看bz2压缩文件内容使用bzcat命令。
6.4 xz压缩工具
xz压缩不常用,它消耗的资源更多,压缩比更高。
[root@localhost d6z]# cp 1.txt 3.txt
[root@localhost d6z]# mv 2.txt.out 2.txt
[root@localhost d6z]# du -sh *
17M 1.txt
4.1M 1.txt.gz
17M 2.txt
17M 3.txt
[root@localhost d6z]# bzip2 2.txt
[root@localhost d6z]# ls
1.txt 1.txt.gz 2.txt.bz2 3.txt
[root@localhost d6z]# xz 3.txt
[root@localhost d6z]# du -sh *
17M 1.txt
4.1M 1.txt.gz
1.4M 2.txt.bz2
52K 3.txt.xz
特别是文本文档有太多的重复内容,xz压缩是非常好用的。
参数与gzip相同,这里不再赘述。压缩级别默认是-6,有时候使用-9反而得到的文件比-6更大。