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

时间:2012-02-09 15:31:28

标签: shell find quoting command-substitution

我想在命令替换中使用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

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

3 个答案:

答案 0 :(得分:3)

点击find result in an array,然后运行command "${array[@]}" some other params

答案 1 :(得分:0)

可能printf动作更适合包含在替换中(仅限GNU find):

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

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

答案 2 :(得分:-2)

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

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

刚试过

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

所有工作都按预期进行