tar命令的實用詳解(C參數和排除文件 --exclude)


一、tar:從壓縮包中解壓出指定文件

[root@d176 test]# tar ztf nrpe-2.12.tar.gz |grep src
nrpe-2.12/src/
nrpe-2.12/src/.cvsignore
nrpe-2.12/src/Makefile.in
nrpe-2.12/src/check_nrpe.c
nrpe-2.12/src/nrpe.c
nrpe-2.12/src/snprintf.c
nrpe-2.12/src/utils.c
[root@d176 test]# tar zxvf nrpe-2.12.tar.gz nrpe-2.12/src        //解壓
nrpe-2.12/src/
nrpe-2.12/src/.cvsignore
nrpe-2.12/src/Makefile.in
nrpe-2.12/src/check_nrpe.c
nrpe-2.12/src/nrpe.c
nrpe-2.12/src/snprintf.c
nrpe-2.12/src/utils.c
[root@d176 test]# ls
bijiao httpd.conf.bak_2015-07-12 locl nrpe-2.12 nrpe-2.12.tar.gz server.xml txt
[root@d176 test]# ls nrpe-2.12
src
[root@d176 test]# ls nrpe-2.12/src/
check_nrpe.c Makefile.in nrpe.c snprintf.c utils.c

[root@d176 test]# tar zxvf nrpe-2.12.tar.gz nrpe-2.12/src -C /root/hhhhhhhhhh/         //指定-C參數不行
nrpe-2.12/src/
nrpe-2.12/src/.cvsignore
nrpe-2.12/src/Makefile.in
nrpe-2.12/src/check_nrpe.c
nrpe-2.12/src/nrpe.c
nrpe-2.12/src/snprintf.c
nrpe-2.12/src/utils.c

或另外一種方式=============>

[root@d176 hhhhhhhhhh]# tar  zxvf  /root/test/nrpe-2.12.tar.gz nrpe-2.12/src          //進入到要解壓的目標目錄
nrpe-2.12/src/
nrpe-2.12/src/.cvsignore
nrpe-2.12/src/Makefile.in
nrpe-2.12/src/check_nrpe.c
nrpe-2.12/src/nrpe.c
nrpe-2.12/src/snprintf.c
nrpe-2.12/src/utils.c
[root@d176 hhhhhhhhhh]# ls
nrpe-2.12
[root@d176 hhhhhhhhhh]# ls nrpe-2.12/
src
[root@d176 hhhhhhhhhh]# ls nrpe-2.12/src/
check_nrpe.c Makefile.in nrpe.c snprintf.c utils.c

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

二、tar:-C參數.

也可以排除目錄與文件一起混合使用,如:

[root@lee ~]# tar -cvf test.tgz test/ --exclude dir1 --exclude a.log --exclude *.jpg
test/
test/b.txt
test/dir2/
test/b.log
test/a.txt

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

三、tar:壓縮解壓排除指定文件/目錄/文件類型等.

問題:在/home/usr1目錄下,想要打包/home/usr2目錄中的文件file2,應該使用什么樣的tar命令?

解答1:
$ tar -cvf file2.tar /home/usr2/file2
tar: Removing leading '/' from members names
home/usr2/file2
該命令可以將/home/usr2/file2文件打包到當前目錄下的file2.tar中,需要注意的是:使用絕對路徑標識的源文件,在用tar命令壓縮后,文件名連同絕對路徑(這里是home/usr2/,根目錄'/'被自動去掉了)一並被壓縮進來。使用tar命令解壓縮后會出現以下情況:
$ tar -xvf file2.tar
$ ls
…… …… home …… …… 
解壓縮后的文件名不是想象中的file2,而是home/usr2/file2。

解答2:
$ tar -cvf file2.tar -C /home/usr2 file2
該命令中的-C dir參數,將tar的工作目錄從當前目錄改為/home/usr2,將file2文件(不帶絕對路徑)壓縮到file2.tar中。注意:-C dir參數的作用在於改變工作目錄,其有效期為該命令中下一次-C dir參數之前。
使用tar的-C dir參數,同樣可以做到在當前目錄/home/usr1下將文件解壓縮到其他目錄,例如:
$ tar -xvf file2.tar -C /home/usr2
而tar不用-C dir參數時是無法做到的:
$ tar -xvf file2.tar /home/usr2
tar: /tmp/file: Not found in archive
tar: Error exit delayed from previous errors


免責聲明!

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



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