tar备份时不显示目录结构

使用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 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值