tar备份时不显示目录结构


使用tar备份文件的时候,如果不进入要备份的目录的进行备份的话,会显示绝对路径(默认会去掉“/”,避免解压之后覆盖源文件),但是,这种目录结构并不一定是我们需要的。

[www.bkjia.com@linux]$ ls /tmp/temp/temp
a  b  c
[www.bkjia.com@linux]$ tar -cvf a.tar /tmp/temp/temp
tar: Removing leading `/' from member names  #默认会去掉“/”
/tmp/temp/temp/
/tmp/temp/temp/c
/tmp/temp/temp/b
/tmp/temp/temp/a
[www.bkjia.com@linux]$ tar -tvf a.tar  #去掉根目录“/”之后,依旧保留路径
drwxr-xr-x billing_dx/BILLING 0 2014-03-06 14:44 tmp/temp/temp/
-rw-r--r-- billing_dx/BILLING 0 2014-03-06 14:41 tmp/temp/temp/c
-rw-r--r-- billing_dx/BILLING 0 2014-03-06 14:41 tmp/temp/temp/b
-rw-r--r-- billing_dx/BILLING 0 2014-03-06 14:41 tmp/temp/temp/a

如果希望不包含上级目录结构的话,有下面两种办法:
1、进入到要备份的目录,进行备份
这种方法有限制,要是同时对多个目录进行打包的话,比较麻烦

2、通过使用-C参数
使用上面的例子,假如我们要备份的目录是"/tmp/temp/temp",那我们可以使用下面的方式备份
tar -cvf 备份文件名 -C 要进入的临时文件夹 要备份的文件或者文件夹

注:-C 是临时切换工作目录,-P是绝对路径,在这里只用到-C参数就行了

举例如下:

[www.bkjia.com@linux]$ tar -cvf a.tar -C /tmp/temp/ temp/
temp/
temp/c
temp/b
temp/a
[www.bkjia.com@linux]$ tar -tvf a.tar
drwxr-xr-x billing_dx/BILLING 0 2014-03-06 14:44 temp/
-rw-r--r-- billing_dx/BILLING 0 2014-03-06 14:41 temp/c
-rw-r--r-- billing_dx/BILLING 0 2014-03-06 14:41 temp/b
-rw-r--r-- billing_dx/BILLING 0 2014-03-06 14:41 temp/a

这样就不显示目录结构了。

问题就是切换到要备份的目录中之后,不可以使用“*”保存所有的文件,如下:

因为使用*号会去替换当前目录下的文件,当前目录下因为有“a.log  a.sh  a.tar”文件,但是在目录“/tmp/temp/temp”中不存在这些文件,所以导致出现了上述错误。

希望清楚的人给与指导,谢谢!

相关内容

    暂无相关文章