首页 > 其他 > 详细

tar备份时不显示目录结构

时间:2014-03-07 02:56:01      阅读:479      评论:0      收藏:0      [点我收藏+]
使用tar备份文件的时候,如果不进入要备份的目录的进行备份的话,会显示绝对路径(默认会去掉“/”,避免解压之后覆盖源文件),但是,这种目录结构并不一定是我们需要的。
[billing_dx@bmcs1 temp]$ ls /tmp/temp/temp
a  b  c
[billing_dx@bmcs1 temp]$ 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
[billing_dx@bmcs1 temp]$ 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参数就行了 
扩展阅读: http://www.chinaunix.net/old_jh/4/541176.html 


举例如下:
[billing_dx@bmcs1 temp]$ tar -cvf a.tar -C /tmp/temp/ temp/
temp/
temp/c
temp/b
temp/a
[billing_dx@bmcs1 temp]$ 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”中不存在这些文件,所以导致出现了上述错误。


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


扩展阅读: http://blog.csdn.net/chinalinuxzend/article/details/3860113 
                     http://www.chinaunix.net/old_jh/4/541176.html 

tar备份时不显示目录结构,布布扣,bubuko.com

tar备份时不显示目录结构

原文:http://blog.csdn.net/yang15225094594/article/details/20628189

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!