tar 使用總結


今天需要備份數據,使用tar命令,總結一下。

壓縮命令:

tar -zvvf ticket-data-intgration.tar.gz ticket-data-intgration

壓縮但是不包含某個文件夾

tar  --exclude /home/q/ticket-data-intgration/log --exclude /home/q/ticket-data-intgration/cache -zvvf ticket-data-intgration.tar.gz /home/q/ticket-data-intgration

查看tar包的文件

tar -tzvf ticket-data-intgration.tar.gz

錯誤:/bin/tar: Removing leading `/' from member names

首先應該明確:linux系統中,使用tar對文件打包時,一般不建議使用絕對路徑。使用絕對路徑打包時如果不指定相應的參數,tar會產生一句警告信息:”tar: Removing leading `/’ from member names”,並且實際產生的壓縮包會將絕對路徑轉化為相對路徑。比如:

root@queen ~ # tar -czvf robin.tar.gz /home/robin
tar: Removing leading `/' from member names
/home/robin/
/home/robin/file1
/home/robin/file2
/home/robin/file3
root@queen ~ # tar -tzvf robin.tar.gz
drwxr-xr-x robin/root        0 2009-11-10 18:51:31 home/robin/
-rw-r--r-- robin/root        0 2009-11-10 18:51:28 home/robin/file1
-rw-r--r-- robin/root        0 2009-11-10 18:51:30 home/robin/file2
-rw-r--r-- robin/root        0 2009-11-10 18:51:31 home/robin/file3
root@queen ~ #

這樣的一個壓縮包,如果我們再去解開,就會當前目錄(也即此例中的“~”)下再新建出“./home/robin/” 兩級目錄。對於這樣的壓縮包,解壓方法是使用參數 “-C”指解壓的目錄為根目錄(“/”):tar -xzvf robin.tar.gz -C /

更為方便的方法是在打包和解開的時候都使用參數 -P

root@queen ~ # tar -czvPf robin.tar.gz /home/robin/
/home/robin/
/home/robin/file1
/home/robin/file2
/home/robin/file3
root@queen ~ # tar tzvf robin.tar.gz
drwxr-xr-x robin/root        0 2009-11-10 18:51:31 /home/robin/
-rw-r--r-- robin/root        0 2009-11-10 18:51:28 /home/robin/file1
-rw-r--r-- robin/root        0 2009-11-10 18:51:30 /home/robin/file2
-rw-r--r-- robin/root        0 2009-11-10 18:51:31 /home/robin/file3
root@queen ~ # tar -xzvPf robin.tar.gz
/home/robin/
/home/robin/file1
/home/robin/file2
/home/robin/file3
root@queen ~ #

注意:上面-P的大小寫和位置都不能改變


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM