在命令替换中使用 find(1) 并用空格引用文件名

发布于 2025-01-04 14:27:34 字数 501 浏览 7 评论 0原文

我想在命令替换中使用 find ,其中返回的文件名包含空格。我需要什么选项才能正确引用文件名?我尝试了 -print0,但它在 shell 本身中不起作用。

例如:

command $(find . -type f) some other params

我也尝试过 -exec echo "{}" \;,但这也没有帮助。


如果我使用 set -x 来显示 shell 扩展和实际执行的命令,我会得到:

$ command `find -type f -printf \"%p\"\ ` some other params
++ find -type f -printf '"%p" '
+ command '"./file_with' 'blanks"' '"./another' 'file"' some other params

单引号来自哪里以及为什么它们应用于每个“单词”?

I'd like to use find inside a command substitution, where the returned filenames contain whitespace. What option do I need so it correctly quotes the filenames? I tried -print0, but it will not work in the shell itself.

example:

command $(find . -type f) some other params

I also tried with -exec echo "{}" \;, but that was of no help either.


If I use set -x to display shell expansion and the actual command which is executed I get:

$ command `find -type f -printf \"%p\"\ ` some other params
++ find -type f -printf '"%p" '
+ command '"./file_with' 'blanks"' '"./another' 'file"' some other params

Where are the single quotation marks coming from and why are they applied to each "word"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事与诗 2025-01-11 14:27:34

查找结果放入数组,然后运行命令“${array[@]}”一些其他参数

Put the find result in an array, and run command "${array[@]}" some other params.

萌梦深 2025-01-11 14:27:34

也许 printf 操作更适合包含在替换中(不过仅限 GNU find):

command $(find . -type f -printf \"%P\"\ ) some other params

%P 占位符是文件名减去find 的参数,因此在 find . 以外的情况下,您可能需要 %p 来代替。

Maybe the printf action is more amenable to being contained in a substitution (GNU find only, though):

command $(find . -type f -printf \"%P\"\ ) some other params

The %P placeholder is the filename minus the argument to find, so in cases other than find ., you'd probably want %p instead.

冷了相思 2025-01-11 14:27:34
find /what/ever -name "what ever" -exec echo "\{\}" \;

在这里工作(Ubuntu 10.04 默认 gterm 与 bash)

刚刚尝试了

find /bin -name ls -exec \{\} -lah \;
`find /bin -name ls -exec echo \{\} \;` -lah
MYCMD=`find /bin -name ls -exec echo \{\} \;` && $MYCMD -lah
MYCMD=$(`find /bin -name ls -exec echo \{\} \;` -lah)  && echo $MYCMD
MYCMD=$(`find /bin -name ls` -lah)  && echo $MYCMD

所有预期的工作

find /what/ever -name "what ever" -exec echo "\{\}" \;

works here (Ubuntu 10.04 default gterm with bash)

Just tried

find /bin -name ls -exec \{\} -lah \;
`find /bin -name ls -exec echo \{\} \;` -lah
MYCMD=`find /bin -name ls -exec echo \{\} \;` && $MYCMD -lah
MYCMD=$(`find /bin -name ls -exec echo \{\} \;` -lah)  && echo $MYCMD
MYCMD=$(`find /bin -name ls` -lah)  && echo $MYCMD

all work as expected

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文