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

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

选项-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 10月 27 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 10月 27 13:28 file10

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

7、tar 打包压缩文件

tar [OPTION...] [FILE]...
命令gzip和bzip2均不支持压缩目录(虽然gzip可以用选项-r到目录内去压缩,但仍无法压缩目录),用tar命令可以将目录归档,然后利用压缩命令进行压缩:

[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 10月 28 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表示创建打包文件,-f tmp.tar表示指定打包文件名为tmp.tar,后面跟被打包目录名tmp/。

选项-t列出归档内容
选项-v详细地列出处理的文件

[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 用于压缩。