焦油:创建档案排除除一个目录 [英] Tar: create archive exclude directories except one
问题描述
我有一些带有文件的目录
I have some directories with some files
dir_archive/somedir1
dir_archive/somedir2
dir_archive/somedir3
dir_archive/mydir
dir_archive/mydir/excludedir1
dir_archive/mydir/excludedir2
dir_archive/mydir/excludedir3
dir_archive/mydir/many_other_directories...
dir_archive/mydir/my_archive_dir
我要创建tar(gz)存档dir_archive.tar.gz,并排除所有文件和目录
I want create tar(gz) archive dir_archive.tar.gz with all files and directories exclude
dir_archive/mydir/excludedir1
dir_archive/mydir/excludedir2
dir_archive/mydir/excludedir3
dir_archive/mydir/many_other_directories...
但包含
dir_archive/mydir/my_archive_dir
我不想为每个目录使用--exclude
I dont want use --exclude
for each directory
tar cvfz dir_archive.tar.gz --exclude=dir_archive/mydir/excludedir1 --exclude=dir_archive/mydir/excludedir2 --exclude=dir_archive/mydir/excludedir3
我尝试使用--add-file
,但是不起作用:
I try use --add-file
, but it doesn't work:
tar cvfz dir_archive.tar.gz --exclude=dir_archive/mydir --add-file=dir_archive/mydir/my_archive_dir dir_archive
存在一些简单的方法吗?谢谢
Exists some simple way? Thanks
推荐答案
一种方法是首先排除mydir
,然后附加my_archive_dir
One way would be first excluding the mydir
and then appending the my_archive_dir
tar cvf dir_archive.tar --exclude=dir_archive/mydir dir_archive
tar rvf dir_archive.tar dir_archive/mydir/my_archive_dir
gzip dir_archive.tar
不幸的是,附加功能不适用于压缩存档.
Unfortunately appending doesn't work with zipped archives.
--exclude
选项采用模式作为参数,因此,如果要排除的目录名称相似,则可以避免使用它们,并且仍包含存档目录
The --exclude
option takes a pattern as argument, so if the names of the dirs to be excluded are similar, you can avoid them and still include the archive dir
tar cvfz dir_archive.tar.gz --exclude=dir_archive/mydir/exclude* dir_archive
还可以创建一个文件,其中包含要包含的所有文件的名称,然后使用选项-T
或--files-from
将该列表提供给tar(或以类似的方式列出要排除的文件,并提供列表,其中包含选项-X
).
It is also possible to create a file with names of all the files that you want included and give that list to tar with option -T
or --files-from
(or in similar fashion list the files to be excluded and give the list with option -X
).
filelist.txt:
dir_archive
dir_archive/temp1
dir_archive/mydir
dir_archive/mydir/temp2
dir_archive/mydir/my_archive_dir
dir_archive/mydir/my_archive_dir/temp7
tar cvfz dir_archive.tar.gz --no-recursion --files-from filelist.txt
这篇关于焦油:创建档案排除除一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!