Linux tar命令exclude選項排除指定文件或目錄


在linux中可以用tar打包目錄以方便傳輸or備份,我們先來看一個例子

test 文件夾有如下文件

 1 [root@lee ~]# ll test
 2 總用量 8
 3 -rw-r--r--. 1 root root    0 4月  14 22:18 a.jpg
 4 -rw-r--r--. 1 root root    0 4月  14 22:25 a.log
 5 -rw-r--r--. 1 root root    0 4月  14 22:18 a.txt
 6 -rw-r--r--. 1 root root    0 4月  14 22:18 b.jpg
 7 -rw-r--r--. 1 root root    0 4月  14 22:25 b.log
 8 -rw-r--r--. 1 root root    0 4月  14 22:18 b.txt
 9 drwxr-xr-x. 2 root root 4096 4月  14 22:18 dir1
10 drwxr-xr-x. 2 root root 4096 4月  14 22:18 dir2

 

打包


 1 [root@lee ~]#  tar -cvf test.tgz test/
 2 test/
 3 test/b.jpg
 4 test/b.txt
 5 test/dir2/
 6 test/b.log
 7 test/dir1/
 8 test/dir1/b.txt
 9 test/dir1/a.txt
10 test/a.jpg
11 test/a.txt
12 test/a.log

 

這樣是打包全部文件,我們需要排除jpg文件可以這么弄


 1 [root@lee ~]#  tar -cvf test.tgz test/ --exclude *.jpg
 2 test/
 3 test/b.txt
 4 test/dir2/
 5 test/b.log
 6 test/dir1/
 7 test/dir1/b.txt
 8 test/dir1/a.txt
 9 test/a.txt
10 test/a.log
11 [root@lee ~]#

 

這樣,就會把jpg后綴的文件都排除了,包括子目錄!

如果是多個后綴類型需要被排除可以在后面添加,無限制


1 [root@lee ~]#  tar -cvf test.tgz test/ --exclude *.txt --exclude *.jpg
2 test/
3 test/dir2/
4 test/b.log
5 test/dir1/
6 test/a.log
7 [root@lee ~]#

 

以上是匹配排除某個文件類型后綴,也可以直接指定文件名


 1 [root@lee ~]#  tar -cvf test.tgz test/ --exclude a.txt 
 2 test/
 3 test/b.jpg
 4 test/b.txt
 5 test/dir2/
 6 test/b.log
 7 test/dir1/
 8 test/dir1/b.txt
 9 test/a.jpg
10 test/a.log
11 [root@lee ~]#

 

或者指定目錄


 1 [root@lee ~]#  tar -cvf test.tgz test/ --exclude dir1
 2 test/
 3 test/b.jpg
 4 test/b.txt
 5 test/dir2/
 6 test/b.log
 7 test/a.jpg
 8 test/a.txt
 9 test/a.log
10 [root@lee ~]#

 

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


1 [root@lee ~]#  tar -cvf test.tgz test/ --exclude dir1 --exclude a.log --exclude *.jpg
2 test/
3 test/b.txt
4 test/dir2/
5 test/b.log
6 test/a.txt
7 [root@lee ~]#

 


免責聲明!

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



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