Linux tar/gzip 排除某些文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17998490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
tar/gzip excluding certain files
提问by user1654183
I have a directory with many sub-directories. In some of those sub-directories I have files with *.asc
extension and some with *.xdr
.
我有一个包含许多子目录的目录。在其中一些子目录中,我的文件*.asc
扩展名为*.xdr
.
I want to create a SINGLE tarball/gzip file which maintains the directory structure but excludes all files with the *.xdr
extension.
我想创建一个单一的 tarball/gzip 文件,它维护目录结构但排除所有带有*.xdr
扩展名的文件。
How can I do this?
我怎样才能做到这一点?
I did something like find . -depth -name *.asc -exec gzip -r9 {} +
but this gzips every *.asc
file individually which is not what I want to do.
我做了类似的事情,find . -depth -name *.asc -exec gzip -r9 {} +
但这*.asc
不是我想要做的,而是单独压缩每个文件。
采纳答案by Allolex
You need to use the --exclude
option:
您需要使用该--exclude
选项:
tar -zc -f test.tar.gz --exclude='*.xdr' *
tar -zc -f test.tar.gz --exclude='*.xdr' *
回答by cars10m
gzip will always handle files individually. If you want a bundled archive you will have to tar the files first and then gzip the result, hence you will end up with a .tar.gz file or .tgz for short.
gzip 将始终单独处理文件。如果你想要一个捆绑的存档,你必须先对文件进行 tar 压缩,然后对结果进行 gzip,因此你最终会得到一个 .tar.gz 文件或简称 .tgz。
To get a better control over what you are doing, you can first find
the files using the command you already posted (with -print instead of the gzip command) and put them into a file, then use this file (=filelist.txt) to instruct tar with what to archive
为了更好地控制你在做什么,你可以首先find
使用你已经发布的命令(使用 -print 而不是 gzip 命令)将文件放入一个文件中,然后使用这个文件(=filelist.txt)来指示 tar 要存档的内容
tar -T filelist.txt -c -v -f myarchive.tar