tar命令


功能说明

    tar是tape archive的缩写,是Linux下经常使用的归档工具,经常看到文件名以.tar和tgz结尾的文件,它们分别是用普通的tar命令归档的文件和用gzip归档的文件。tar归档文件可以由许多独立的文件、一个或多个目录层次或者两者的混合组合而成。用法如下:

  tar [主选项+辅助选项] 文件或目录

常用参数

选项说明
-c新建文件
-C指定位置解压
-r列出归档文件的内容
-t列出档案文件中已经归档的文件列表
-x从打包的档案文件中还原文件
-u更新档案文件,用新建文件替换档案中的原始文件
-z调用gzip命令在打包过程中压缩/解压文件 .tar.gz
-j调用bzip2命令在打包过程中压缩/解压文件
-J调用xz命令在打包过程中压缩/解压文件
-w在还原文件时,把所有文件的修改时间设定为现在时间
-Z用compress命令过滤档案
-f"-f"选项后面紧跟档案文件的存储设备,默认是磁盘,需要指定档案文件名;如果是磁带,只需指定磁带设备名即可。注意,在"-f"选项之后不能再跟任何其他选项,也就是说"-f"必须是tar命令的最后一个选项
-v指定在创建归档文件过程中,显示各个归档文件的名称
-p在文件归档的过程中,保持文件的属性不发生变化
-N "yyyy/mm/dd"在指定日期之后的文件才会打包到档案文件中
--exclude file在打包过程中,不将指定的file文件打包
-X把不打包的文件写到文件里,配合X使用如:tar zcvf 1.tar.gz paichu.list ./shell/
常用组合命令
zcvf打包.tar.gz
zxvf解压.tar.gz

示例                    

    使用cf压缩,把当前目录下的test文件夹打包成test.tar,为便于演示,提前把app里的文件复制到test里一份

[root@c7 app]# tar -cf test.tar test/

[root@c7 app]# ll -h

总用量 432K

-rw-r--r--. 1 root root  181 11月 22 17:34 99.sh

-rw-------. 1 root root  12K 11月 22 14:08 messages

-rw-r--r--. 1 root root 1.2K 11月 22 15:17 messages2.gz

-rw-r--r--. 1 root root 1.2K 11月 22 14:36 messages.gz

-rw-r--r--. 1 root root 141K 11月 23 10:17 shell.txt

-rw-r--r--. 1 root root  48K 11月 23 10:17 shell.txt.bz2

drwxr-xr-x. 2 root root 4.0K 11月 23 17:07 test

-rw-r--r--. 1 root root 210K 11月 23 17:21 test.tar


    使用xf解压使用-C把文件解压到指定目录下

[root@c7 app]# mkdir test1                       #创建目标目录test1

[root@c7 app]# pwd

/root/app

[root@c7 app]# tar xf test.tar -C ./test1/     #解压到当前目录test1下

[root@c7 app]# ls ./test1/test/                  #验证查看

99.sh  messages  messages2.gz  messages.gz  shell.txt  shell.txt.bz2

    将test目录下的文件打包并压缩,打包成test2.tar.gz

   将test目录下的文件打包并压缩,打包成test2.tar.gz2

[root@c7 app]# tar zcvf test2.tar.gz test          #调用gzip打包成test2.tar.gz

test/

test/99.sh

test/messages

test/messages2.gz

test/messages.gz

test/shell.txt

test/shell.txt.bz2

[root@c7 app]# tar jcvf test2.tar.bz2 test        #调用bzip2打包成test2.tar.bz2

test/

test/99.sh

test/messages

test/messages2.gz

test/messages.gz

test/shell.txt

test/shell.txt.bz2

[root@c7 app]# ll -h

...略

drwxr-xr-x. 2 root root 4.0K 11月 23 17:07 test

drwxr-xr-x. 3 root root   17 11月 23 17:24 test1

-rw-r--r--. 1 root root 105K 11月 24 09:06 test2.tar.bz2

-rw-r--r--. 1 root root 105K 11月 24 09:05 test2.tar.gz

-rw-r--r--. 1 root root 210K 11月 23 17:21 test.tar

    使用tf、ztvf参数,查看压缩包的内容

[root@c7 app]# tar tf test2.tar.gz 

[root@c7 app]# tar ztvf test2.tar.gz 

drwxr-xr-x root/root         0 2016-11-23 17:07 test/

-rw-r--r-- root/root       181 2016-11-23 17:07 test/99.sh

-rw------- root/root     11595 2016-11-23 17:07 test/messages

-rw-r--r-- root/root      1149 2016-11-23 17:07 test/messages2.gz

-rw-r--r-- root/root      1202 2016-11-23 17:07 test/messages.gz

-rw-r--r-- root/root    143538 2016-11-23 17:07 test/shell.txt

-rw-r--r-- root/root     48218 2016-11-23 17:07 test/shell.txt.bz2

    使用zxvf参数,解压tar.gz和tgz文件,这个最常用了

[root@c7 tmp]# pwd                               #到/tmp目录下,在哪里解压文件,解压后的文件就会在当前目录下

/tmp

[root@c7 tmp]# tar zxvf /root/app/test2.tar.gz 

test/

test/99.sh

test/messages

test/messages2.gz

test/messages.gz

test/shell.txt

test/shell.txt.bz2

[root@c7 tmp]# ll -h test/

总用量 216K

-rw-r--r--. 1 root root  181 11月 23 17:07 99.sh

-rw-------. 1 root root  12K 11月 23 17:07 messages

-rw-r--r--. 1 root root 1.2K 11月 23 17:07 messages2.gz

-rw-r--r--. 1 root root 1.2K 11月 23 17:07 messages.gz

-rw-r--r--. 1 root root 141K 11月 23 17:07 shell.txt

-rw-r--r--. 1 root root  48K 11月 23 17:07 shell.txt.bz2

    使用zcvpf参数,将/tmp/test目录打包到当前目录下,并保存每个文件的权限

[root@c7 tmp]# tar zcvpf test3.tar.gz test/

test/

test/99.sh

test/messages

test/messages2.gz

test/messages.gz

test/shell.txt

test/shell.txt.bz2

[root@c7 tmp]# tar ztvf test3.tar.gz        #权限和上面的演示相同

drwxr-xr-x root/root         0 2016-11-23 17:07 test/

-rw-r--r-- root/root       181 2016-11-23 17:07 test/99.sh

-rw------- root/root     11595 2016-11-23 17:07 test/messages

-rw-r--r-- root/root      1149 2016-11-23 17:07 test/messages2.gz

-rw-r--r-- root/root      1202 2016-11-23 17:07 test/messages.gz

-rw-r--r-- root/root    143538 2016-11-23 17:07 test/shell.txt

-rw-r--r-- root/root     48218 2016-11-23 17:07 test/shell.txt.bz2

    在家目录下,仅解压/tmp/test3.tar.gz压缩文件中的test/shell.txt文件

[root@c7 ~]# pwd

/root

[root@c7 ~]# tar zxvf /tmp/test3.tar.gz test/shell.txt

test/shell.txt

[root@c7 ~]# ll

总用量 8

-rw-------. 1 root root 1104 4月  13 2016 anaconda-ks.cfg

drwxr-xr-x. 4 root root 4096 11月 24 09:06 app

drwxr-xr-x. 2 root root   22 11月 24 09:34 test

[root@c7 ~]# ll test/

总用量 144

-rw-r--r--. 1 root root 143538 11月 23 17:07 shell.txt

    使用-N指定日期,打包/root/app目录中2016/11/24以后的文件

[root@c7 tmp]# tar -N "2016/11/24" -zcvf log.tar.gz /root/app

    使用-exclude,打包文件时,排除指定文件

打包时排除expr1.sh文件

[root@redmine ~]# ls ./shell/

expr1.sh  judge_int.sh

[root@redmine ~]# tar zcvf sh.tar.gz --exclude=./shell/expr1.sh ./shell/

./shell/

./shell/judge_int.sh


打包时排除.sh的所有文件

[root@redmine ~]# ls ./shell/

1.txt  expr1.sh  judge_int.sh

[root@redmine ~]# tar zcvf sh.tar.gz --exclude=./shell/*.sh ./shell/     

./shell/

./shell/1.txt

[root@redmine ~]# tar tf sh.tar.gz 

./shell/

./shell/1.txt

    使用-C,指定位置解压

[root@redmine ~]# tar xf sh.tar.gz -C /tmp/

[root@redmine ~]# ls /tmp/shell/

1.txt


学习自:

高俊峰《循序渐进Linux》