不断前行,方可不被淘汰

4、gzip压缩或解压文件

gzip [OPTION]... [FILE]...

命令后直接跟文件时,表示压缩该文件:

[root@centos7 temp]# ls -l file1*
-rw-r--r-- 1 root root 132 10月 27 13:28 file10
-rw-r--r--
1 root root  64 10月 27 15:06 file11
-rw-r--r--
1 root root  22 10月 26 21:31 file12
-rw-r--r--
1 root root 137 10月 12 16:42 file13
[root@centos7 temp]# [root@centos7 temp]# gzip file10 file11 file12 file13
[root@centos7 temp]# ls -l file1*                     -rw-r--r-- 1 root root  75 10月 27 13:28 file10.gz
-rw-r--r-- 1 root root  49 10月 27 15:06 file11.gz
-rw-r--r-- 1 root root  44 10月 26 21:31 file12.gz
-rw-r--r-- 1 root root 109 10月 12 16:42 file13.gz

压缩后的文件以.gz结尾,gzip是不保留源文件的

选项-d表示解压缩

[root@centos7 temp]# gzip -d *.gz
[root@centos7 temp]
# ls -l file1*
-rw-r--r-- 1 root root 132 10月 27 13:28 file10
-rw-r--r--
1 root root  64 10月 27 15:06 file11
-rw-r--r--
1 root root  22 10月 26 21:31 file12
-rw-r--r--
1 root root 137 10月 12 16:42 file13

选项-r可以递归地步入目录并压缩上面的文件

选项-n指定压缩级别,n为从1-9的数字。1为最快压缩,但压缩比最小;9的压缩速率最慢,但压缩比最大。默认时n为6。

[root@centos7 temp]# gzip -r9 ./tmp

cmd搜索文件命令_linux搜索文件内容命令_linux搜索文件命令

当gzip后没有文件或文件为-时,将从标准输入读取并压缩:

[root@centos7 temp]# echo "hello world" | gzip >hello.gz
[root@centos7 temp]# ls -l *.gz
-rw-r--r-- 1 root root 32 11月  1 16:40 hello.gz

注意事例中gzip的输出被重定向到文件hello.gz中,假如对此文件进行解压,将会生成文件hello。倘若被重定向的文件后缀不是.gz,文件名在被改成.gz后缀之前将不能被解压。

5、zcat将压缩的文件内容输出到标准输出

[root@centos7 temp]# zcat hello.gz 
hello world [root@centos7 temp]#

zcat读取被gzip压缩的文件,只需文件格式正确,不须要文件名具有.gz的后缀。

6、bzip2压缩解压文件

bzip2 [OPTION]... [FILE]...

命令bzip2和gzip类似都是压缩命令,只是使用的压缩算法不一样node.js安装linux,一般bzip2的压缩比较高。本命令默认同样不保留源文件,默认文件名后缀为.bz2:

[root@centos7 temp]# bzip2 file11
[root@centos7 temp]# ls -l file11.bz2
-rw-r--r-- 1 root root 61 1027 15:06 file11.bz2

选项-k可使源文件保留:

[root@centos7 temp]# bzip2 -k file10 
[root@centos7 temp]# ls -l file10*
-rw-r--r-- 1 root root 132 1027 13:28 file10 -rw-r--r-- 1 root root  96 1027 13:28 file10.bz2

选项-d表示解压(若存在源文件则报错):

[root@centos7 temp]# bzip2 -d file10.bz2
bzip2: Output file file10 already exists. [root@centos7 temp]# bzip2 -d file11.bz2
[root@centos7 temp]# ls -l file11
-rw-r--r-- 1 root root 64 1027 15:06 file11

选项-f表示强制覆盖源文件:

[root@centos7 temp]# bzip2 -d -f file10.bz2
[root@centos7 temp]# ls -l file10*
-rw-r--r-- 1 root root 132 1027 13:28 file10

选项-n和gzip用法一致,表示压缩比。

7、tar打包压缩文件

tar [OPTION...] [FILE]...

命令gzip和bzip2均不支持压缩目录(尽管gzip可以用选项-r到目录内去压缩,但仍未能压缩目录),用tar命令可以将目录归档linux搜索文件内容命令,之后借助压缩命令进行压缩:

[root@centos7 temp]# tar -cf tmp.tar tmp/
[root@centos7 temp]# ls -l
总用量 18256
drwxr-xr-x 2 root root        6 11月  1 16:23 abcd -rwxr-xr-x 1 root root       12 1028 17:24 test.sh drwxr-xr-x 2 root root   425984 11月  1 17:08 tmp -rw-r--r-- 1 root root 18001920 11月  1 17:17 tmp.tar

事例中选项-c表示创建打包文件,-ftmp.tar表示指定打包文件名为tmp.tar,前面跟被打包目录名tmp/。

选项-t列举归档内容

选项-v详尽地列举处理的文件

linux搜索文件内容命令_linux搜索文件命令_cmd搜索文件命令

[root@centos7 temp]# ls -l abcd.tar 
-rw-r--r-- 1 root root 10240 11月  2 08:58 abcd.tar [root@centos7 temp]# tar -tvf abcd.tar
drwxr-xr-x root/root         0 2016-11-02 08:57 abcd/
-rw-r--r-- root/root         6 2016-11-02 08:57 abcd/file10 -rw-r--r-- root/root         6 2016-11-02 08:57 abcd/file11 -rw-r--r-- root/root         6 2016-11-02 08:57 abcd/file12 -rw-r--r-- root/root         6 2016-11-02 08:57 abcd/file13

选项-u更新归档文件(update)。

[root@centos7 temp]# touch abcd/file15
[root@centos7 temp]# tar uvf abcd.tar abcd
abcd/file15 [root@centos7 temp]# tar tvf abcd.tar
drwxr-xr-x root/root         0 2016-11-02 08:57 abcd/
-rw-r--r-- root/root         6 2016-11-02 08:57 abcd/file10 -rw-r--r-- root/root         6 2016-11-02 08:57 abcd/file11 -rw-r--r-- root/root         6 2016-11-02 08:57 abcd/file12 -rw-r--r-- root/root         6 2016-11-02 08:57 abcd/file13 -rw-r--r-- root/root         0 2016-11-02 09:07 abcd/file15

选项-x对归档文件进行提取操作。(解包)

[root@centos7 temp]# rm -rf abcd/
[root@centos7 temp]# tar -xvf abcd.tar
abcd/ abcd/file10 abcd/file11 abcd/file12 abcd/file13 abcd/file15 [root@centos7 temp]# ls abcd   #这里是按两次tab键的补全结果
abcd/     abcd.tar   [root@centos7 temp]#

选项-O解压文件至标准输出

[root@centos7 temp]# tar -xf abcd.tar -O 
hello hello hello hello [root@centos7 temp]#  #注意这里输出了每个归档文件的内容
[root@centos7 temp]# tar -xf abcd.tar -O | xargs echo
hello hello hello hello [root@centos7 temp]#

选项-p保留文件权限(用于解包时)。

选项-j、-J、-z用于压缩。

其中-j使用命令bzip2,-J使用命令xzredhat linux下载,-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的压缩比最高linux搜索文件内容命令,命令gzip的压缩比最低。在执行压缩文件时,压缩时间也是我们审视的一个重要诱因。默认时,使用gzip最快,xz最慢。

linux搜索文件命令_linux搜索文件内容命令_cmd搜索文件命令

对于这三种格式的压缩文件进行解压,只需将选项中-c换成-x即可。

选项-XFILE排除匹配文件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

选项-CDIR改变至目录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 1116 15:26 file12 -rw-r--r-- 1 root root 6 1116 15:26 file13

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

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

[root@centos7 temp]# tar zxf tmp.tar.gz file -O
BLOG ADDRESS IS "https://segmentfault.com/blog/learnning"
[root@centos7 temp]#

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

相关链接:

1、

2、

3、

4、

5、

6、

7、

课程信息:

课程:第13期Python实战班

时间:2017年2月19日(周一)

Author

这篇优质的内容由TA贡献而来

刘遄

《Linux就该这么学》书籍作者,RHCA认证架构师,教育学(计算机专业硕士)。

发表回复