I have nearly 6000 files to edit using following sed command. its taking longer time.
does any one have an idea how to make following commands faster.
find ./ -type f -exec sed -i -e 's/old word/new word/g' {} \;
Similar to @JonathanLeffler's strategy of running one invocation of sed for multiple files, but you can also use xargs to run multiple threads in parallel, taking advantage of multiple processors.
find ./ -type f -print0 | xargs -0 -n 100 -P 8 sed -i .bak -e 's/old word/new word/g'
There is really only one way to speed it up and that is to make it execute sed
less often, which you do by replacing the \;
with +
. This makes find
group file names up to a convenient limit, roughly like xargs
would do, but without the nuisances of having to rely on GNU -print0
and -0
options (to find
and xargs
respectively — though they are also available in the BSD find
and xargs
available on Mac OS X). It is also POSIX standard notation.
Note that you are making backups of your affected files with the suffix -e
because the Mac version of sed
insists on an argument for the -i
option, and the -e
happens to be there to be used, and the script does not have to be prefixed with -e
.
Hence, this command should work faster:
find ./ -type f -exec sed -i .bak -e 's/old word/new word/g' {} +
How much faster is more debatable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With