Linux中使用tar压缩命令排除文件

众所周知tar命令是在Linux系统中最为常用来解压缩文件的命令之一,之前大部分时候都直接用它来压缩备份或转移的文件内容,因此也未过多关注过它在压缩时的其它可选参数使用。但最近在转移文件遇到其占用空间比较大,考虑到里面有些内容并不是必须,于是想到如何来使用tar命令参数来实现,经过多次尝试,找到了个解决办法——使用exclude-from参数,可灵活控制不需要压缩文件,然后顺手做个记录分享。

注:当排除的内容并不多时,也可以直接使用exclude参数会更方便些。

exclude-from 参数的使用说明相对简单,其后面跟的是排除文件的路径。但是需要注意如下2种不同的情况:

  1. 当排除文件的路径是相对路径时,压缩路径无论是相对路径还是绝对路径都可以;
  2. 当排除文件的路径是绝对路径时,压缩路径也必须是绝对路径。

接下来我们就准备个测试的文件夹和文件,整个目录结构如下,其中以exclude起头的文件夹或文件便是需要排除的内容:

1
2
3
4
5
6
7
8
9
tar_excludes_demo/
├── exclude_file.txt
├── exclude_folder
│   └── tmp.txt
├── folder1
│   └── tmp.txt
├── folder2
│   └── tmp.txt
└── readme.txt

根据参数说明的两种情况进行分类的测试验证:

示例1 相对路径

我们先把需要排除的内容写到文本文件里面,如下:

1
2
3
excludes_relative.txt
tar_excludes_demo/exclude_file.txt
tar_excludes_demo/exclude_folder

然后使用不同相对路径压缩办法测试下效果,加上v参数可以验证是否正确:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 任意目录压缩
tar --exclude-from /root/tar_demo/excludes_relative.txt -czvf tar_excludes_demo.tar.gz /root/tar_demo/tar_excludes_demo
tar: Removing leading '/' from member names
/root/tar_demo/tar_excludes_demo/
/root/tar_demo/tar_excludes_demo/folder1/
/root/tar_demo/tar_excludes_demo/folder1/tmp.txt
/root/tar_demo/tar_excludes_demo/readme.txt
/root/tar_demo/tar_excludes_demo/folder2/
/root/tar_demo/tar_excludes_demo/folder2/tmp.txt

# 父目录外面压缩,同时去除父目录路径
tar --exclude-from tar_demo/excludes_relative.txt  -czvf tar_excludes_demo.tar.gz -C tar_demo tar_excludes_demo
tar_excludes_demo/
tar_excludes_demo/folder1/
tar_excludes_demo/folder1/tmp.txt
tar_excludes_demo/readme.txt
tar_excludes_demo/folder2/
tar_excludes_demo/folder2/tmp.txt

# 父目录里面压缩
tar --exclude-from excludes_relative.txt -czvf tar_excludes_demo.tar.gz tar_excludes_demo
tar_excludes_demo/
tar_excludes_demo/folder1/
tar_excludes_demo/folder1/tmp.txt
tar_excludes_demo/readme.txt
tar_excludes_demo/folder2/
tar_excludes_demo/folder2/tmp.txt

观察压缩的执行过程,可清晰的看出使用相对路径,无论是哪个位置或方式压缩文件都能达到预期要求,成功排除不需要的内容。接下来我们看看绝对路径的效果:

示例2 绝对路径

同样是先把需要排除的内容写到文本文件里面,如下:

1
2
3
excludes_absolute.txt
/root/tar_demo/tar_excludes_demo/exclude_file.txt
/root/tar_demo/tar_excludes_demo/exclude_folder

然后使用绝对路径压缩办法测试下效果,加上v参数可以验证是否正确:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 绝对路径压缩
tar --exclude-from /root/tar_demo/excludes_absolute.txt -czvf tar_excludes_demo.tar.gz /root/tar_demo/tar_excludes_demo
tar: Removing leading '/' from member names
/root/tar_demo/tar_excludes_demo/
/root/tar_demo/tar_excludes_demo/folder1/
/root/tar_demo/tar_excludes_demo/folder1/tmp.txt
/root/tar_demo/tar_excludes_demo/readme.txt
/root/tar_demo/tar_excludes_demo/folder2/
/root/tar_demo/tar_excludes_demo/folder2/tmp.txt

# 尝试相对路径压缩
tar --exclude-from tar_demo/excludes_absolute.txt -czvf tar_excludes_demo.tar.gz tar_demo/tar_excludes_demo
tar_demo/tar_excludes_demo/
tar_demo/tar_excludes_demo/exclude_folder/
tar_demo/tar_excludes_demo/exclude_folder/tmp.txt
tar_demo/tar_excludes_demo/exclude_file.txt
tar_demo/tar_excludes_demo/folder1/
tar_demo/tar_excludes_demo/folder1/tmp.txt
tar_demo/tar_excludes_demo/readme.txt
tar_demo/tar_excludes_demo/folder2/
tar_demo/tar_excludes_demo/folder2/tmp.txt


# 尝试绝对路径转移方式
tar --exclude-from /root/tar_demo/excludes_absolute.txt -czvf tar_excludes_demo.tar.gz -C /root/tar_demo tar_excludes_demo
tar_excludes_demo/
tar_excludes_demo/exclude_folder/
tar_excludes_demo/exclude_folder/tmp.txt
tar_excludes_demo/exclude_file.txt
tar_excludes_demo/folder1/
tar_excludes_demo/folder1/tmp.txt
tar_excludes_demo/readme.txt
tar_excludes_demo/folder2/
tar_excludes_demo/folder2/tmp.txt

观察压缩的执行过程,可清晰的看出使用绝对路径的方式,只能支持绝对路径这一种压缩方法。

通过对比相对路径绝对路径两种不同方式的压缩文件排除方法效果,在此更建议使用相对路径的方式,它可以灵活支持各种压缩方式的执行,同时也不用写那很长串的路径参数,另外要注意的点就是当排除的内容是文件夹时,排除的路径只要写到文件夹名称即可,万万不可在最后加上/结尾,切记!!!