tar备份时不显示目录结构

本文发布时间: 2019-Mar-22
使用tar备份文件的时候,如果不进入要备份的目录的进行备份的话,会显示绝对路径(默认会去掉“/”,避免解压之后覆盖源文件),但是,这种目录结构并不一定是我们需要的。[billing_dx@bmcs1 temp]$ ls /tmp/temp/tempa b c[billing_dx@bmcs1 temp]$ tar -cvf a.tar /tmp/temp/temptar: 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/ctemp/btemp/a[billing_dx@bmcs1 temp]$ tar -tvf a.tardrwxr-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”中不存在这些文件,所以导致出现了上述错误。希望清楚的人给与指导,谢谢!


(以上内容不代表本站观点。)
---------------------------------
本网站以及域名有仲裁协议。
本網站以及域名有仲裁協議。

2024-Mar-04 02:10pm
栏目列表