Find And Replace In Multiple Files From The Terminal In Linux

Posted by Weston Ganger on May 27, 2015

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:

Posted in Linux and Tagged with linux grep find-and-replace 

Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts