文章轉載自:freefly的博客,對原作者表示感謝。
問題:
tar czf xx.tgz /xxx/xxx/A 然後希望 xx.tgz 或 xx.tar.gz 裏面就直接A 這個目錄 不希望加前導xxx/xxx,我知道可以先cp 這個目錄到同一個目錄再打包,不過想知道可以不可以不用另外cp到同一個目錄
答案 1:
使用-C指定相對路徑,如:tar czf xx.tgz -C /xxx/xxx A
eg:
[root@localhost home]# ls /root
30 anaconda-ks.cfg Desktop diff.txt h.txt install.log install.log.syslog Mail mbox test z.txt
[root@localhost home]# tar czvf root.tar.gz -C /root/ test
[root@localhost home]# ls
root.tar.gz
接下來驗證一下:
[root@localhost home]# ls
root.tar.gz
[root@localhost home]# tar zxvf root.tar.gz
[root@localhost home]# ls
root.tar.gz test
[root@localhost test]# ls
aa.sh a.txt b.txt file2 files.txt iftest.sh myfile myfile.log ping2.sh shell aa.txt blockcharacterfile.sh file1 file3 if.sh mce.sh myfile1 owner.sh ping3.sh tcs.sh
答案2:
你進入那個目錄在打包不就好了
eg:我要打包/home目錄下面的內容
[root@localhost /]# cd home
[root@localhost home]# ls
test
[root@localhost home]# tar -zcvf test.tar.gz ./test/*
[root@localhost home]# ls
test test.tar.gz
[root@localhost home]# ll
total 8
drwxr-xr-x 3 root root 4096 Mar 6 16:49 test
-rw-r--r-- 1 root root 3702 Apr 15 10:52 test.tar.gz