使用 -exec 选项查询 Solaris find 命令
我想创建 tar 文件,其中包含执行 find
命令产生的所有输出文件。
我尝试了以下命令:
find . \(-name "*.log" -o -name "*.log.*" \) -mtime +7 -exec tar cvf test.tar.gz {} \;
但它只包含 test.tar
文件中最后找到的文件。如何包含 test.tar
文件中的所有文件?
问候 柴塔尼亚
I want to create tar file with all the output files resulting from executing find
command.
I tried the following command:
find . \(-name "*.log" -o -name "*.log.*" \) -mtime +7 -exec tar cvf test.tar.gz {} \;
But it is including only the last found file in the test.tar
file. How to include all files in test.tar
file?
Regards
Chaitanya
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用命令行替换:
它的作用是运行 $() 中的命令并使输出成为外部命令的命令行参数。
这使用了更现代的 bash 表示法。如果您不使用 bash,您还可以使用反引号,它应该适用于大多数 shell:
虽然反引号更便于移植,但如果您需要嵌套命令行替换,则 $() 表示法更容易。
Use command line substitution:
What this does is run the command in $() and makes the output the command line arguments of the outer command.
This uses the more modern bash notation. If you are not using bash, you can also use backticks which should work with most shells:
While backticks are more portable, the $() notation is easier if you need to nest command line substitution.
您希望将 find 找到的文件名通过管道传送到 tar 中。
You want to pipe the file names found by find into tar.
这是因为对于它发现的每个文件,它都运行一个新的 tar 命令,该命令会覆盖上一个命令的 tar 文件。
您可以通过将
\;
更改为+
来使 find 将文件批处理在一起,但如果有更多如果文件无法一次列出,find 仍会运行多个命令,每个命令都会覆盖前一个命令的 tar 文件。您可以通过 xargs 管道输出,但它具有可能多次运行该命令的相同问题。上面推荐的命令行替换是我所知道的最安全的方法,确保 tar 只能调用一次 - 但如果找到太多文件,它可能会给出有关命令行太长的错误。
That's because for every file it finds it is running a new tar command that overwrites the tar file from the previous command.
You can make find batch the files together by changing the
\;
to a+
but if there's morefiles than can be listed at once, find will still run multiple commands, each overwriting the tar file from the previous one. You could pipe the output through xargs but it has the same issue of possibly running the command multiple times. The command line substitution recommended above is the safest way I know of to do it, ensuring that tar can only be called once -- but if too many files are found, it may give an error about the command line being too long.
这个应该同样有效:
注意末尾的“+”与“\;”。
当大量文件与搜索匹配时,采用可靠的方法:
This one should equally work:
Note the "+" at the end vs "\;".
For a reliable way when a very large number of files will match the search: