linux tar 命令


-v 压缩/解压缩的过程中,将正在处理的文件名显示出来(常用)
-f 后面立刻接要被处理的文档名

-c 建立打包文档,
-x 解打包或解压缩
-t 查看打包文档的内容

一般 -c,-x,-t不可能出现在同一串指令中

-j 以bzip2进行压缩或解压缩。文档名最好:*.tar.bz2
-z 以gzip进行压缩或解压缩。 文档名最好:*.tar.gz
-C 目录 :解压缩时使用,用来特定解压缩的目录

压缩:tar -czv -f filename.tar.gz 要被压缩的档案或目录名称
查询:tar -tzv -f filename.tar.gz
解压缩tar -xzv -f filename.tar.gz -C 解压到的目录

创建一个测试目录:/tartest,新建几个文件
[root@05 tartest]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 10 10:51 a.txt
-rw-r--r-- 1 root root 0 Apr 10 10:51 b.txt
-rw-r--r-- 1 root root 0 Apr 10 10:51 c.txt


压缩:
[root@05 /]# tar -cv -f tartest.tar tartest/
tartest/
tartest/b.txt
tartest/c.txt
tartest/a.txt

会生成一个tar文件
-rw-r--r--   1 root root    10240 Apr 10 10:54 tartest.tar


查看tar文件内容
[root@05 /]# tar -tv -f tartest.tar 
drwxr-xr-x root/root         0 2014-04-10 10:53:57 tartest/
-rw-r--r-- root/root         0 2014-04-10 10:51:08 tartest/b.txt
-rw-r--r-- root/root         0 2014-04-10 10:51:14 tartest/c.txt
-rw-r--r-- root/root         0 2014-04-10 10:51:04 tartest/a.txt

解压缩该文件到新建目录tartest1

[root@05 /]# mkdir tartest1
[root@05 /]# tar -xv -f tartest.tar -C tartest1/
tartest/
tartest/b.txt
tartest/c.txt
tartest/a.txt


这是tartset1下有一个tartest目录,tartset目录下有3个文件


只解压单一文档

首先清空tartest1目录
[root@05 tartest1]# rm -rf *
查看tartest.tar中文件
[root@05 /]# tar -tv -f tartest.tar 
drwxr-xr-x root/root         0 2014-04-10 10:53:57 tartest/
-rw-r--r-- root/root         0 2014-04-10 10:51:08 tartest/b.txt
-rw-r--r-- root/root         0 2014-04-10 10:51:14 tartest/c.txt
-rw-r--r-- root/root         0 2014-04-10 10:51:04 tartest/a.txt

只解压最后一个文件
引用
[root@05 /]# tar -xv -f tartest.tar tartest/a.txt -C tartest1
tartest/a.txt



打包某目录,不包含该目录下某个文档
使用--exclude参数

在tartest目录下新建一个ddd目录,建一个d.txt文件
[root@05 tartest]# mkdir ddd
[root@05 tartest]# ll
total 4
-rw-r--r-- 1 root root    0 Apr 10 10:51 a.txt
-rw-r--r-- 1 root root    0 Apr 10 10:51 b.txt
-rw-r--r-- 1 root root    0 Apr 10 10:51 c.txt
drwxr-xr-x 2 root root 4096 Apr 10 11:08 ddd
[root@05 tartest]# cd ddd
[root@05 ddd]# ll
total 0
[root@05 ddd]# touch d.txt
[root@05 ddd]# cd ..
[root@05 tartest]# ll
total 4
-rw-r--r-- 1 root root    0 Apr 10 10:51 a.txt
-rw-r--r-- 1 root root    0 Apr 10 10:51 b.txt
-rw-r--r-- 1 root root    0 Apr 10 10:51 c.txt
drwxr-xr-x 2 root root 4096 Apr 10 11:08 ddd
[root@05 tartest]# cd ..

打包tartest目录,不打包其下ddd目录
[root@05 /]# tar -cv -f tartest.tar --exclude=ddd tartest
tartest/
tartest/b.txt
tartest/c.txt
tartest/a.txt

查看打包后的tar文件,确实没有ddd目录
[root@05 /]# tar -tv -f tartest.tar 
drwxr-xr-x root/root         0 2014-04-10 11:08:37 tartest/
-rw-r--r-- root/root         0 2014-04-10 10:51:08 tartest/b.txt
-rw-r--r-- root/root         0 2014-04-10 10:51:14 tartest/c.txt
-rw-r--r-- root/root         0 2014-04-10 10:51:04 tartest/a.txt



备份比某个时刻还新的文档
使用--newer-mtime参数
[root@05 /]# tar -cv -f new.tar --newer-mtime="2014/04/11" /tartest
tar: Treating date `2014/04/11' as 2014-04-11 00:00:00 + 0 nanoseconds
tar: Removing leading `/' from member names
/tartest/
tar: /tartest/b.txt: file is unchanged; not dumped
tar: /tartest/c.txt: file is unchanged; not dumped
tar: /tartest/a.txt: file is unchanged; not dumped
/tartest/ddd/
tar: /tartest/ddd/d.txt: file is unchanged; not dumped
[root@05 /]# tar -tv -f new.tar 
drwxr-xr-x root/root         0 2014-04-10 11:23:19 tartest/
drwxr-xr-x root/root         0 2014-04-10 11:08:49 tartest/ddd/

查看生成的tar文件,发现没有文档被打包,只有两个空目录,
因为都比设定的时间早

猜你喜欢

转载自nullpoint.iteye.com/blog/2042951
今日推荐