linux文件搜索及其它基础命令介绍(3)

2019-09-23 09:15:16于丽

其中-j使用命令bzip2,-J使用命令xz,-z使用命令gzip分别将归档文件进行压缩解压处理(命令tar后的选项可以省略-):

[root@centos7 temp]# tar zcf tmp.tar.gz tmp
[root@centos7 temp]# tar jcf tmp.tar.bz2 tmp
[root@centos7 temp]# tar Jcf tmp.tar.xz tmp
[root@centos7 temp]# du -sh tmp*
70M  tmp
28K  tmp.tar.bz2
180K tmp.tar.gz
40K  tmp.tar.xz
[root@centos7 temp]#

本例中分别使用三种压缩格式进行压缩,可以看到使用命令bzip2的压缩比最高,命令gzip的压缩比最低。在执行压缩文件时,压缩时间也是我们考量的一个重要因素。默认时,使用gzip最快,xz最慢。
对于这三种格式的压缩文件进行解压,只需将选项中-c换成-x即可。

选项-X FILE 排除匹配文件FILE中所列模式的文件:

[root@centos7 abcd]# cat file
file10
file13
[root@centos7 abcd]# tar -X file -cf file.tar file*
[root@centos7 abcd]# tar -tvf file.tar 
-rw-r--r-- root/root  14 2016-11-02 10:10 file
-rw-r--r-- root/root   6 2016-11-02 10:02 file11
-rw-r--r-- root/root   6 2016-11-02 10:02 file12
-rw-r--r-- root/root   0 2016-11-02 09:07 file15

注意文件FILE中支持通配符匹配:

[root@centos7 abcd]# cat file
file1[2-3]
[root@centos7 abcd]# tar -X file -cf file.tar file*
[root@centos7 abcd]# tar -tvf file.tar 
-rw-r--r-- root/root  11 2016-11-02 10:20 file
-rw-r--r-- root/root   6 2016-11-02 10:02 file10
-rw-r--r-- root/root   6 2016-11-02 10:02 file11
-rw-r--r-- root/root   0 2016-11-02 09:07 file15

选项-C DIR改变至目录DIR(用于解包时):

[root@centos7 temp]# tar zxf tmp.tar.gz -C abcd
[root@centos7 temp]# ls -l abcd/
总用量 688
-rw-r--r-- 1 root root  11 11月 2 10:20 file
-rw-r--r-- 1 root root  6 11月 2 10:02 file10
-rw-r--r-- 1 root root  6 11月 2 10:02 file11
-rw-r--r-- 1 root root  6 11月 2 10:02 file12
-rw-r--r-- 1 root root  6 11月 2 10:02 file13
-rw-r--r-- 1 root root  0 11月 2 09:07 file15
drwxr-xr-x 2 root root 425984 11月 1 17:08 tmp

只解压指定文件:

[root@centos7 temp]# tar zxvf tmp.tar.gz -C abcd/ file1[23]
file12
file13
[root@centos7 temp]# ls -l abcd
总用量 12
-rw-r--r-- 1 root root 6 11月 16 15:26 file12
-rw-r--r-- 1 root root 6 11月 16 15:26 file13

注意这里解压时,指定文件不能在选项-C之前

如不想解压压缩包,但想查看压缩包中某个文件的内容时,可以使用如下技巧:

[root@centos7 temp]# tar zxf tmp.tar.gz file -O
BLOG ADDRESS IS "//www.jb51.net/article/101263.htm"
[root@centos7 temp]# 

本文讲述了linux中关于文件搜索和归档压缩等相关的命令及部分选项用法,都是在系统管理过程中经常要使用的,需熟练使用。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易采站长站。