I’m trying to run the expand shell command on all files found by a find command. I’ve tried -exec and xargs but both failed. Can anyone explain me why? I’m on a mac for the record.
find . -name "*.php" -exec expand -t 4 {} > {} \;
This just creates a file {} with all the output instead of overwriting each individual found file itself.
find . -name "*.php" -print0 | xargs -0 -I expand -t 4 {} > {}
And this just outputs
4 {}
xargs: 4: No such file or directory
Your command does not work for two reasons.
find. That means that the shell will redirectfinds output into the file{}.expandcommand. So it’s not possible to redirect a command’s output into the input file.Unfortunately
expanddoesn’t allow to write it’s output into a file. So you have to use output redirection. If you usebashyou could define afunctionthat executesexpand, redirects the output into a temporary file and move the temporary file back over the original file. The problem is thatfindwill run a new shell to execute theexpandcommand.But there is a solution:
You are exporting the function
expand_functo sub shells usingexport -f. And you don’t executeexpanditself usingfind -execbut you execute a newbashthat executes the exportedexpand_func.