我试图在find
命令find的所有文件上运行expand shell命令。 我试过了–exec和xargs但都失败了。 谁能解释我为什么? 我在logging的Mac上。
find . -name "*.php" -exec expand -t 4 {} > {} \;
这只是创build一个包含所有输出的文件,而不是覆盖每个find的文件本身。
find . -name "*.php" -print0 | xargs -0 -I expand -t 4 {} > {}
而这只是输出
4 {} xargs: 4: No such file or directory
你的命令不起作用有两个原因。
find
。 这意味着shell会将find
的输出重定向到文件{}
。 expand
命令读取之前将被写入。 所以将命令的输出重定向到输入文件是不可能的。 不幸的是, expand
不允许将其输出写入文件。 所以你必须使用输出重定向。 如果你使用bash
你可以定义一个执行expand
的function
,将输出重定向到一个临时文件并将临时文件移回到原始文件。 问题是find
会运行一个新的shell来执行expand
命令。
但有一个解决方案:
expand_func () { expand -t 4 "$1" > "$1.tmp" mv "$1.tmp" "$1" } export -f expand_func find . -name \*.php -exec bash -c 'expand_func {}' \;
您正在使用export -f
将函数expand_func
导出到子shell。 而且您不使用find -exec
执行expand
而是执行一个新的bash
来执行导出的expand_func
。
“扩大”是不是真的值得麻烦。 你可以使用sed来代替:
find . -name "*.php" | xargs sed -i -e 's/\t/ /g'