Find and Replace in all files

23 August 2013

You want to change a string with another one in some directory in your project.

Here is a one liner to do that:

for spec_file in $(ag -g _spec.rb spec/); do sed -i.bak 's/john/jane/g' $spec_file; done

A few caveats:

(If you know how to do the replacement with awk, or something similar, please tell me in the comments.)

Your text editor might already have a Find & Replace in project functionality. But maybe it does not. And surely, the command line version is most flexible.