在linux中可以用tar打包目录 file 文件夹有如下文件[root@cloud ~]# ll file
总用量 8
-rw-r--r--. 1 root root 0 4月 14 22:18 a.jpg
-rw-r--r--. 1 root root 0 4月 14 22:25 a.log
-rw-r--r--. 1 root root 0 4月 14 22:18 a.txt
-rw-r--r--. 1 root root 0 4月 14 22:18 b.jpg
-rw-r--r--. 1 root root 0 4月 14 22:25 b.log
-rw-r--r--. 1 root root 0 4月 14 22:18 b.txt
drwxr-xr-x. 2 root root 4096 4月 14 22:18 dir1
drwxr-xr-x. 2 root root 4096 4月 14 22:18 dir2
完全打包
[root@cloud ~]# tar -cvf file.tgz file/
file/
file/b.jpg
file/b.txt
file/dir2/
file/b.log
file/dir1/
file/dir1/b.txt
file/dir1/a.txt
file/a.jpg
file/a.txt
file/a.log
这样是打包全部文件,我们需要排除jpg文件可以这么弄
[root@cloud ~]# tar -cvf file.tgz file/ --exclude *.jpg
file/
file/b.txt
file/dir2/
file/b.log
file/dir1/
file/dir1/b.txt
file/dir1/a.txt
file/a.txt
file/a.log
[root@cloud ~]#
这样,就会把jpg后缀的文件都排除了,包括子目录! 如果是多个后缀类型需要被排除可以在后面添加,无限制
[root@cloud ~]# tar -cvf file.tgz file/ --exclude *.txt --exclude *.jpg
file/
file/dir2/
file/b.log
file/dir1/
file/a.log
[root@cloud ~]#
以上是匹配排除某个文件类型后缀,也可以直接指定文件名
[root@cloud ~]# tar -cvf file.tgz file/ --exclude a.txt
file/
file/b.jpg
file/b.txt
file/dir2/
file/b.log
file/dir1/
file/dir1/b.txt
file/a.jpg
file/a.log
[root@cloud ~]#
或者指定目录
[root@cloud ~]# tar -cvf file.tgz file/ --exclude dir1
file/
file/b.jpg
file/b.txt
file/dir2/
file/b.log
file/a.jpg
file/a.txt
file/a.log
[root@cloud ~]#
也可以排除目录与文件一起混合使用,如:
[root@cloud ~]# tar -cvf file.tgz file/ --exclude dir1 --exclude a.log --exclude *.jpg
file/
file/b.txt
file/dir2/
file/b.log
file/a.txt
[root@cloud ~]#