第八章:文档的压缩和打包课后习题

第八章:文档的压缩和打包课后习题

1.gzip命令和bzip2命令能否直接压缩目录呢?
不可以。下面我也dir1这个目录做实验:

[root@zl_cloud ~]# ls
1.txt  anaconda-ks.cfg  dir1  dira  dirb  test  test1  test2
[root@zl_cloud ~]# gzip dir1
gzip: dir1 is a directory -- ignored
[root@zl_cloud ~]# bzip2 dir1
bzip2: Input file dir1 is a directory.
[root@zl_cloud ~]# 

2.请快速写出使用gzip和bzip2压缩和解压一个文件的命令。
分别使用gzip和bzip2压缩文件:

[root@zl_cloud test]# gzip 1.txt
[root@zl_cloud test]# bzip2 2.txt
[root@zl_cloud test]# ls
1.txt.gz      2.txt.bz2

分别使用gzip和bzip2解压缩文件:

[root@zl_cloud test]# gzip -d 1.txt.bz2
[root@zl_cloud test]# bzip2 -d 2.txt.bz2 
[root@zl_cloud test]# ls
1.txt      2.txt
[root@zl_cloud test]# 

3.tar在打包时,如果想排除多个文件或者目录,该如何操作?
比如说我想打包test111,排除里面的test222目录和1.txt文件:

[root@zl_cloud test]# cd test111
[root@zl_cloud test111]# ll
总用量 4
-rw-r--r--. 1 root root 0 3月   7 06:56 1.txt
-rw-r--r--. 1 root root 6 3月   7 06:56 2.txt
drwxr-xr-x. 2 root root 6 3月   7 07:18 test222
[root@zl_cloud test111]# cd ..
[root@zl_cloud test]# tar -cvf test111.tar --exclude test222 test111
test111/
test111/2.txt
test111/1.txt
[root@zl_cloud test]# tar -cvf test111.tar --exclude test222 --exclude 1.txt test111
test111/
test111/2.txt
[root@zl_cloud test]# 

4.请试验:如果不加-是否正确,如tar zcvf 1.tar.gz 1.txt 2.txt?
没问题。

[root@zl_cloud dirb]# ll
总用量 0
-rw-r--r--. 1 root root 0 3月   7 13:02 1.txt
-rw-r--r--. 1 root root 0 3月   7 13:02 2.txt
[root@zl_cloud dirb]# tar zcvf 1.tar.gz 1.txt 2.txt
1.txt
2.txt
[root@zl_cloud dirb]#

5.如何使用tar打包和解包格式为.tar.gz和.tar.bz2的压缩包?
打包:

[root@zl_cloud dirb]# tar -zcvf 1.tar.gz 1.txt 2.txt
1.txt
2.txt
[root@zl_cloud dirb]# tar -jcvf 1.tar.bz2 1.txt
1.txt

解包:

[root@zl_cloud dirb]# tar -zxvf 1.tar.gz
1.txt
2.txt
[root@zl_cloud dirb]# tar -jxvf 1.tar.bz2 
1.txt
[root@zl_cloud dirb]# 

6.找一个大点的文件.使用tar分别把这个文件打成.tar.gz和.tar.bz2格式的压缩包,比较一下哪个包会更小,并由此判断是gzip压缩效果好还是bzip2压缩效果好。
bzip2压缩会更小一点,bzip2的压缩效果好。

7.使用tar打包并压缩时,默认压缩级别为几?如何能够改变压编级别呢?(提示:tar本身没有这个功能,可以尝试拆分打包和压缩。)
使用tar打包并压缩时默认压缩机别为6。可以先打包,再压缩,压缩的时候设置级别即可。(我设置的级别为9)

[root@zl_cloud dirb]# tar -cvf change.tar 2.txt
2.txt
[root@zl_cloud dirb]# ls
2.txt  change.tar
[root@zl_cloud dirb]# bzip2 -9 change.tar 
[root@zl_cloud dirb]# ls
2.txt  change.tar.bz2

猜你喜欢

转载自blog.csdn.net/zhang_ZERO/article/details/104773107