Chr*_*isR 4 unix linux expand find xargs
我正在尝试对命令找到的所有文件运行expand shell find命令.我试过-exec和xargs但都失败了.谁能解释我为什么?我在Mac上备案.
find . -name "*.php" -exec expand -t 4 {} > {} \;
这只是创建一个{}包含所有输出的文件,而不是覆盖每个单独找到的文件本身.
find . -name "*.php" -print0 | xargs -0 -I expand -t 4 {} > {}
这只是输出
4 {}
xargs: 4: No such file or directory
Run Code Online (Sandbox Code Playgroud)
您的命令不起作用有两个原因.
find.这意味着shell会将find输出重定向到文件中{}.expand命令读取之前,也会写入文件.因此无法将命令的输出重定向到输入文件中.不幸的是expand,不允许将其输出写入文件.所以你必须使用输出重定向.如果使用,bash您可以定义function执行的expand,将输出重定向到临时文件,并将临时文件移回原始文件.问题是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 {}' \;
Run Code Online (Sandbox Code Playgroud)
您正在使用函数将函数导出expand_func到子shell export -f.并且您不执行expand自己使用find -exec但执行新的bash执行导出expand_func.