Find And Replace In Multiple Files From The Terminal In Linux

Posted by Weston Ganger

Sometimes you may find yourself needing to find and replace a word recursively inside a folder. This can be a big pain and while it usually is best to manually go through each yourself to be sure you are making the correct changes, in the right circumstances it can be pretty awesome to just do it automatically.

Heres a one liner to do just that. You may need to install xargs.


grep -rl 'string1' somedir/ | xargs sed -i 's/string1/string2/g'

# an example, change hello to goodbye
grep -rl 'hello' apps/my_app/ | xargs sed -i 's/hello/goodbye/g'


Related External Links:

Article Topic:Software Development - Linux

Date:May 27, 2015

Recommended Posts