问题描述
与 .gitignore
类似,是否可以将子目录中的 .tarignore
文件从存档中排除
In a similar way to .gitignore
, is it possible to do that a .tarignore
file in a subdirectory makes it excluded from archive when doing
tar cfjvh backup.tar.bz2 .
(我知道 tar
的 --exclude
参数,但这里不是我想要的).
(I know the --exclude
parameter of tar
but here it's not what I want).
推荐答案
使用 --exclude-ignore=.tarignore
选项使 Tar 使用 .tarignore
文件的方式与 git 使用 的方式类似.gitignore
文件.使用与您通常在 .gitignore
文件中使用的语法相同的语法,因此如果您想忽略特定目录中的所有子目录和文件,请添加一个 .gitignore
文件用 *
到那个目录.例如:
Using the --exclude-ignore=.tarignore
option causes Tar to use .tarignore
files in a similar way that git uses .gitignore
files. Use the same syntax as you would usually use in .gitignore
files, so if you want to ignore all sub-directories and files in a particular directory, add a .gitignore
file with *
to that directory. For example:
$ find dir
dir
dir/a
dir/b
dir/c
dir/dir1
dir/dir1/1a
dir/dir1/1b
dir/dir2
dir/dir2/2a
dir/dir2/2b
$ echo "*" > dir/dir1/.tarignore
$ tar -c --exclude-ignore=.tarignore -vjf archive.tar.bz2 dir
dir/
dir/a
dir/b
dir/c
dir/dir1/
dir/dir2/
dir/dir2/2a
dir/dir2/2b
如果您想忽略所有具有空 .tarignore
文件的目录,请查看 --exclude-tag-all=.tarignore
选项.
If you want to ignore all directories that have an empty .tarignore
file, take a look at the --exclude-tag-all=.tarignore
option.
$ find dir
dir
dir/a
dir/b
dir/c
dir/dir1
dir/dir1/1a
dir/dir1/1b
dir/dir2
dir/dir2/2a
dir/dir2/2b
$ touch dir/dir1/.tarignore
$ tar -c --exclude-tag-all=.tarignore -vjf archive.tar.bz2 dir
tar: dir/dir1/: contains a cache directory tag .tarignore; directory not dumped
dir/
dir/a
dir/b
dir/c
dir/dir2/
dir/dir2/2a
dir/dir2/2b
另请查看 --exclude-tag=
和 --exclude-tag=
选项,它们是行为略有不同的变体.
Also take a look at the --exclude-tag=
and --exclude-tag=
options, which are variations which have slightly different behaviors.
这篇关于使用 .tarignore 文件从 tar 存档中排除目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!