歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> tar備份時不顯示目錄結構

tar備份時不顯示目錄結構

日期:2017/2/28 14:35:04   编辑:Linux教程

使用tar備份文件的時候,如果不進入要備份的目錄的進行備份的話,會顯示絕對路徑(默認會去掉“/”,避免解壓之後覆蓋源文件),但是,這種目錄結構並不一定是我們需要的。

[www.linuxidc.com@linux]$ ls /tmp/temp/temp
a b c
[www.linuxidc.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.linuxidc.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.linuxidc.com@linux]$ tar -cvf a.tar -C /tmp/temp/ temp/
temp/
temp/c
temp/b
temp/a
[www.linuxidc.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”中不存在這些文件,所以導致出現了上述錯誤。

希望清楚的人給與指導,謝謝!

Copyright © Linux教程網 All Rights Reserved