bash:find 命令中的复杂测试
我想做类似的事情:
find . -type f -exec test $(file --brief --mime-type '{}' ) == 'text/html' \; -print
但我无法找出引用或转义要测试的参数的正确方法,尤其是 '$(' ... ')' 。
I would like to do something like:
find . -type f -exec test $(file --brief --mime-type '{}' ) == 'text/html' \; -print
but I can't figure out the correct way to quote or escape the args to test, especially the '$(' ... ')' .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能简单地将参数转义以将其传递给
find
。任何 shell 扩展都会在
find
运行之前发生。find
不会通过 shell 传递其参数,因此即使您转义 shell 扩展,所有内容都将被简单地视为test
命令的文字参数,而不是由正如你所期望的那样。实现您想要的效果的最佳方法是编写一个简短的 shell 脚本,该脚本将文件名作为参数,然后使用
-exec
:with
is_html.sh
:如果您确实希望将所有内容都放在一行中,而不使用单独的脚本,则可以直接从
find
调用sh
:You cannot simply escape the arguments for passing them to
find
.Any shell expansion will happen before
find
is run.find
will not pass its arguments through a shell, so even if you escape the shell expansion, everything will simply be treated as literal arguments to thetest
command, not expanded by the shell as you are expecting.The best way to achieve what you want would be to write a short shell script, which takes the filename as an argument, and use
-exec
on that:with
is_html.sh
:If you really want it all on one line, without using a separate script, you can invoke
sh
directly fromfind
:尽管可以将其变成一个被广泛引用的声明,但更冗长一些通常更容易且更清晰:
Although it may be possible to turn it into one wildly quoted statement, it is often easier - and more clear - to be a little more verbose:
请改用“{}”,例如,仅列出文件类型:
Use "{}" instead, for an example this simply lists file types: