Linux 如何tar所有子目录中的某些文件类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18731603/
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
How to tar certain file types in all subdirectories?
提问by user1566515
I want to tar and all .php and .html files in a directory and its subdirectories. If I use
我想在一个目录及其子目录中 tar 和所有 .php 和 .html 文件。如果我使用
tar -cf my_archive *
tar -cf my_archive *
it tars all the files, which I don't want. If I use
它压缩了所有我不想要的文件。如果我使用
tar -cf my_archive *.php *.html
tar -cf my_archive *.php *.html
it ignores subdirectories. How can I make it tar recursively but include only two types of files?
它忽略子目录。如何使其递归 tar 但只包含两种类型的文件?
采纳答案by DeeDee
find ./someDir -name "*.php" -o -name "*.html" | tar -cf my_archive -T -
find ./someDir -name "*.php" -o -name "*.html" | tar -cf my_archive -T -
回答by Robin Sheat
One method is:
一种方法是:
tar -cf my_archive.tar $( find -name "*.php" -or -name "*.html" )
There are some caveats with this method however:
但是,此方法有一些注意事项:
- It will fail if there are any files or directories with spaces in them, and
- it will fail if there are so many files that the maximum command line length is full.
- 如果有任何带有空格的文件或目录,它将失败,并且
- 如果文件太多以至于最大命令行长度已满,它将失败。
A workaround to these could be to output the contents of the find command into a file, and then use the "-T, --files-from FILE" option to tar.
解决这些问题的方法可能是将 find 命令的内容输出到文件中,然后使用“-T,--files-from FILE”选项进行 tar。
回答by Trent Huang
tar -cf my_archive `find ./ | grep '.php\|.html'`
Use "find" and "grep" to get all path of .php and .html files in all directory and its sub-directories. Then pass those path information to tar to compress.
使用“find”和“grep”获取所有目录及其子目录中.php和.html文件的所有路径。然后将这些路径信息传递给 tar 进行压缩。
Please be careful with those symbol ` and '. Note also that this will hit the limit of how many characters your shell will allow on the command line, unlike some of the other answers.
请小心那些符号`和'。另请注意,与其他一些答案不同,这将达到 shell 在命令行上允许的字符数的限制。
回答by Ian Reinhart Geiser
This will handle paths with spaces:
这将处理带空格的路径:
find ./ -type f -name "*.php" -o -name "*.html" -exec tar uvf myarchives.tar {} +
回答by Sairam Krish
To compress multiple files with different patterns, we could separate them with space and add them all to the gz file, like this :
要压缩具有不同模式的多个文件,我们可以用空格分隔它们并将它们全部添加到 gz 文件中,如下所示:
tar -czvf deploy.tar.gz **/Alice*.yml **/Bob*.json
this will add all .yml files that starts with Alice from any sub-directory and add all .json files that starts with Bob from any sub-directory.
这将添加任何子目录中以 Alice 开头的所有 .yml 文件,并添加任何子目录中以 Bob 开头的所有 .json 文件。
回答by Noam Geffen
Put them in a file
把它们放在一个文件中
find . \( -name "*.php" -o -name "*.html" \) -print > files.txt
Then use the file as input to tar, use -I or -T depending on the version of tar you use
然后使用该文件作为 tar 的输入,根据您使用的 tar 版本使用 -I 或 -T
Use h to copy symbolic links
使用 h 复制符号链接
tar cfh my.tar -I files.txt
回答by dmitry_podyachev
find ./ -type f -name "*.php" -o -name "*.html" -printf '%P\n' |xargs tar -I 'pigz -9' -cf target.tgz
find ./ -type f -name "*.php" -o -name "*.html" -printf '%P\n' |xargs tar -I 'pigz -9' -cf target.tgz
for multicore or just for one core:
对于多核或仅用于一个核:
find ./ -type f -name "*.php" -o -name "*.html" -printf '%P\n' |xargs tar -czf target.tgz
find ./ -type f -name "*.php" -o -name "*.html" -printf '%P\n' |xargs tar -czf target.tgz