第八章:文档的压缩与打包
Linux下常见的后缀名所对应的压缩工具。
- .gz:表示由gzip压缩工具压缩的文件;
- .bz2:表示由bzip2压缩工具压缩的文件;
- .tar:表示由tar打包程序打包的文件;(tar并没有压缩功能,只是把一个目录合并成一个文 件)
- .tar.gz:可以理解为先由tar打包,然后再由gzip压缩。
- .tar.bz2:可以理解为先由tar打包,然后再由gzip压缩。
- .tar.xz:可以理解为先由tar打包,然后再由xz压缩。
一、gzip压缩工具(在当前目录下压缩该文件,压缩后源文件会消失。不可压缩目录)
格式:gzip 【-d#】 【filename】,其实#为1~9的数字。
- -d:该参数在解压缩时使用。
- -#:表示压缩等级,1为最差,9为最好,6为默认。
[root@zl_cloud ~]# touch 1.txt
[root@zl_cloud ~]# ll
总用量 4
-rw-r--r--. 1 root root 0 3月 7 06:22 1.txt
-rw-------. 1 root root 955 3月 4 20:23 anaconda-ks.cfg
drwxrwxrw-. 2 root root 6 3月 5 05:33 dir1
drwxrwxrwT. 2 user1 users 41 3月 5 05:53 dira
drwxr-x---. 2 root root 6 3月 5 05:42 dirb
-rw-r--r--. 1 root root 0 3月 5 05:43 test
-rw-rw-rw-. 1 root root 0 3月 5 05:52 test1
-rw-rw-rw-. 1 root root 0 3月 5 05:52 test2
[root@zl_cloud ~]# gzip 1.txt
[root@zl_cloud ~]# ls
1.txt.gz anaconda-ks.cfg dir1 dira dirb test test1 test2
[root@zl_cloud ~]#
可以看到,源文件1.txt消失了。如果把1.txt.gz解压回来的话的话:
[root@zl_cloud ~]# gzip -d 1.txt.gz
[root@zl_cloud ~]# ls
1.txt anaconda-ks.cfg dir1 dira dirb test test1 test2
[root@zl_cloud ~]#
gzip不支持压缩目录,压缩目录的话会报错:
[root@zl_cloud ~]# gzip dir1
gzip: dir1 is a directory -- ignored
[root@zl_cloud ~]#
二、bzip2压缩工具(在当前目录下压缩该文件,压缩后源文件会消失。不可压缩目录)
格式:bzip2 【-dz】 【filename】。
- -z:压缩;级别有1~9,默认级别时9.加不加-z选项都可以压缩文件。
- -d:解压缩;
[root@zl_cloud ~]# bzip2 1.txt
-bash: bzip2: 未找到命令
[root@zl_cloud ~]#
系统没有安装这个,需要安装bzip2库才可以使用此命令。
[root@zl_cloud ~]# yum install -y bzip2
[root@zl_cloud ~]# bzip2 1.txt
[root@zl_cloud ~]# ls
1.txt.bz2 anaconda-ks.cfg dir1 dira dirb test test1 test2
[root@zl_cloud ~]# bzip2 -d 1.txt.bz2
[root@zl_cloud ~]# ls
1.txt anaconda-ks.cfg dir1 dira dirb test test1 test2
[root@zl_cloud ~]#
同样不可以压缩目录:
[root@zl_cloud ~]# bzip2 dir1
bzip2: Input file dir1 is a directory.
[root@zl_cloud ~]#
三、xz压缩工具(在当前目录下压缩该文件,压缩后源文件会消失。不可压缩目录)
格式:xz 【-dz】 【filename】。和bzip2相似。
- -z:压缩;加不加-z选项都可以压缩文件。
- -d:解压缩;
[root@zl_cloud ~]# xz 1.txt
[root@zl_cloud ~]# ls
1.txt.xz anaconda-ks.cfg dir1 dira dirb test test1 test2
[root@zl_cloud ~]# xz -d 1.txt.xz
[root@zl_cloud ~]# ls
1.txt anaconda-ks.cfg dir1 dira dirb test test1 test2
[root@zl_cloud ~]#
同样不可以压缩目录:
[root@zl_cloud ~]# xz dir1
xz: dir1: Is a directory, skipping
[root@zl_cloud ~]#
四、tar打包工具(可打包目录)
tar本身就是一个打包工具,可以把目录打包成一个文件,它把所有文件整合成一个大文件,方便复制或者移动。
格式:tar 【-zjxcvfpP】 【filename】。
- -z:表示同时用gzip压缩;
- -j:表示同时用bzip2压缩;
- -J:表示同时用xz压缩;
- -x:表示解包或者解压缩;
- -t:表示査看tar包里的文件;
- -c:表示建立一个tar包或者压缩文件包;
- -v:表示可视化;
- -f:后面跟文件名(即-f filename,表示压缩后的文件名为filename,或者解圧文件filename。 需要注意的是,如果是多个参数组合的情况下,清把-f参数写到最后面
- -p:表示使用原文件的属性,压缩前什么厲性压缩后还什么属性;(不常用)
- -P:表示可以使用绝对路径,(不常用)
- --exclude filename:表示在打包或压缩时,不要将filename文件包括在内。(不常用)
[root@zl_cloud ~]# cd /test
[root@zl_cloud test]# touch 1.txt
[root@zl_cloud test]# ll
总用量 0
-rw-r--r--. 1 root root 0 3月 7 06:55 1.txt
[root@zl_cloud test]# mkdir test111 //建立test111目录
[root@zl_cloud test]# touch test111/2.txt //建立test111目录下的2.txt文件
[root@zl_cloud test]# echo "nihao" > test111/2.txt //将nihao写入test111/2.txt文件中
[root@zl_cloud test]# cp 1.txt test111/ //复制1.txt文件到test111/目录下
[root@zl_cloud test]# yum install -y tree //安装tree命令,查看目录树形结构
[root@zl_cloud test]# tree .
.
├── 1.txt
└── test111
├── 1.txt
└── 2.txt
1 directory, 3 files
[root@zl_cloud test]# tar -cvf test111.tar test111
test111/
test111/2.txt
test111/1.txt
[root@zl_cloud test]# ls
1.txt test111 test111.tar
[root@zl_cloud test]#
tar不仅可以打包目录,也可以打包文件,打包时可不加-v,表示不可视化:
[root@zl_cloud test]# rm -f test111.tar
[root@zl_cloud test]# tar -cf test.tar test111 1.txt
[root@zl_cloud test]# ls
1.txt test111 test.tar
[root@zl_cloud test]#
不管是打包还是解包,原来的文件是不会删除的,而且它会覆盖当前已经存在的文件或者目录:
[root@zl_cloud test]# tar -xvf test.tar
test111/
test111/2.txt
test111/1.txt
1.txt
[root@zl_cloud test]#
–exclude选项也是不用删除文件,它会自动覆盖:
[root@zl_cloud test]# tar -cvf test111.tar --exclude 1.txt test111
test111/
test111/2.txt
[root@zl_cloud test]#
上例中test111.tar放到了–exclude选项的前面。该选项除了可以排除文件,也可以排除目录:
[root@zl_cloud test]# mkdir test111/test222
[root@zl_cloud test]# tar -cvf test111.tar --exclude test222 test111
test111/
test111/2.txt
test111/1.txt
[root@zl_cloud test]#
1.打包的同时使用gzip压缩
tar可以在打包时直接压缩,它支持gzip、bzip2、xz压缩。使用-z选项可以压缩成gzip格式的文件:
[root@zl_cloud test]# tar -czvf test111.tar.gz test111
test111/
test111/2.txt
test111/1.txt
test111/test222/
[root@zl_cloud test]# ls
1.txt test111 test111.tar test111.tar.gz test.tar
[root@zl_cloud test]#
使用-tf选项,可以查看包或者压缩包的文件列表:
[root@zl_cloud test]# tar -tf test111.tar.gz
test111/
test111/2.txt
test111/1.txt
test111/test222/
[root@zl_cloud test]# tar -tf test.tar
test111/
test111/2.txt
test111/1.txt
1.txt
[root@zl_cloud test]#
使用-zxvf选项,可以解压.tar.gz格式的压缩包:
[root@zl_cloud test]# ls
1.txt test111.tar.gz test.tar
[root@zl_cloud test]# tar -zxvf test111.tar.gz
test111/
test111/2.txt
test111/1.txt
test111/test222/
[root@zl_cloud test]# ls
1.txt test111 test111.tar.gz test.tar
[root@zl_cloud test]#
2.打包的同时使用bzip2压缩
和gzip压缩不同的是,这里使用-cjvf选项压缩:
[root@zl_cloud test]# tar -cjvf test111.tar.bz2 test111
test111/
test111/2.txt
test111/1.txt
test111/test222/
[root@zl_cloud test]# ls
1.txt test111 test111.tar.bz2 test111.tar.gz test.tar
[root@zl_cloud test]#
使用-tf选项来查看压缩包的文件列表:
[root@zl_cloud test]# tar -tf test111.tar.bz2
test111/
test111/2.txt
test111/1.txt
test111/test222/
[root@zl_cloud test]#
使用-jxvf选项来解压.tar.bz2格式的压缩包:
[root@zl_cloud test]# tar -jxvf test111.tar.bz2
test111/
test111/2.txt
test111/1.txt
test111/test222/
[root@zl_cloud test]#
3.打包的同时使用xz压缩
和前面例子压缩不同的是,这里使用-cJvf选项压缩:
[root@zl_cloud test]# tar -cJvf test111.tar.xz test111
test111/
test111/test222/
test111/2.txt
test111/1.txt
[root@zl_cloud test]# ls
1.txt test111 test111.tar.bz2 test111.tar.gz test111.tar.xz test.tar
[root@zl_cloud test]#
使用-tf选项来查看压缩包的文件列表:
[root@zl_cloud test]# tar -tf test111.tar.xz
test111/
test111/test222/
test111/2.txt
test111/1.txt
[root@zl_cloud test]#
使用-Jxvf选项来解压.tar.xz格式的压缩包:
[root@zl_cloud test]# tar -Jxvf test111.tar.xz
test111/
test111/test222/
test111/2.txt
test111/1.txt
[root@zl_cloud test]#
五、使用zip压缩(可压缩目录)
格式:zip 【自定义压缩包名】 【要压缩的目录或文件名】。
[root@zl_cloud test]# zip 1.txt.zip 1.txt
-bash: zip: 未找到命令
[root@zl_cloud test]#
可看出这里没有该命令,则需要yum工具安装它:
[root@zl_cloud ~]# yum install -y zip
[root@zl_cloud test]# ll
总用量 24
-rw-r--r--. 1 root root 0 3月 7 06:55 1.txt
drwxr-xr-x. 3 root root 44 3月 7 07:18 test111
-rw-r--r--. 1 root root 193 3月 7 07:33 test111.tar.bz2
-rw-r--r--. 1 root root 193 3月 7 07:32 test111.tar.gz
-rw-r--r--. 1 root root 228 3月 7 07:36 test111.tar.xz
-rw-r--r--. 1 root root 10240 3月 7 07:07 test.tar
[root@zl_cloud test]# zip 1.txt.zip 1.txt
adding: 1.txt (stored 0%)
[root@zl_cloud test]# zip test111.zip test111/*
adding: test111/1.txt (stored 0%)
adding: test111/2.txt (stored 0%)
adding: test111/test222/ (stored 0%)
[root@zl_cloud test]#
压缩目录时,如果只写目录名,则zip命令只是将二级目录本身压缩。如果想要一并压缩二级目录下的文件,则必须加上-r选项:
[root@zl_cloud test]# cd test111
[root@zl_cloud test111]# ls
1.txt 2.txt test222
[root@zl_cloud test111]# cd ..
[root@zl_cloud test]# zip -r test111.zip test111/
updating: test111/1.txt (stored 0%)
updating: test111/2.txt (stored 0%)
updating: test111/test222/ (stored 0%)
adding: test111/ (stored 0%)
[root@zl_cloud test]#
解压.zip格式文件:
[root@zl_cloud test]# unzip 1.txt.zip
-bash: uzip: 未找到命令
[root@zl_cloud test]#
使用yum工具安装:
[root@zl_cloud test]# yum install -y unzip
[root@zl_cloud test]# ls
1.txt test111 test111.tar.gz test111.zip
1.txt.zip test111.tar.bz2 test111.tar.xz test.tar
[root@zl_cloud test]# unzip 1.txt.zip
Archive: 1.txt.zip
replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
extracting: 1.txt
[root@zl_cloud test]# ls
1.txt test111 test111.tar.gz test111.zip
1.txt.zip test111.tar.bz2 test111.tar.xz test.tar
[root@zl_cloud test]#
六、zcat、bzcat命令的使用
[root@zl_cloud test]# cat 1.txt
abc abc
test
[root@zl_cloud test]# ls
1.txt test111 test111.tar.gz test111.zip
1.txt.zip test111.tar.bz2 test111.tar.xz test.tar
[root@zl_cloud test]# cp 1.txt 2.txt
[root@zl_cloud test]# ls
1.txt 2.txt test111.tar.bz2 test111.tar.xz test.tar
1.txt.zip test111 test111.tar.gz test111.zip
[root@zl_cloud test]# gzip 1.txt
[root@zl_cloud test]# bzip2 2.txt
[root@zl_cloud test]# zcat 1.txt.gz
abc abc
test
[root@zl_cloud test]# bzcat 2.txt.bz2
abc abc
test
[root@zl_cloud test]#
查看xz压缩文件的内容:
[root@zl_cloud test]# cp 1.txt 3.txt
[root@zl_cloud test]# xz 3.txt
[root@zl_cloud test]# ls
1.txt.gz 2.txt.bz2 test111 test111.tar.gz test111.zip
1.txt.zip 3.txt.xz test111.tar.bz2 test111.tar.xz test.tar
[root@zl_cloud test]# xzcat 3.txt.xz
abc abc
test
[root@zl_cloud test]#