How to grep and replace

billtian picture billtian · Mar 14, 2013 · Viewed 362.9k times · Source

I need to recursively search for a specified string within all files and subdirectories within a directory and replace this string with another string.

I know that the command to find it might look like this:

grep 'string_to_find' -r ./*

But how can I replace every instance of string_to_find with another string?

Answer

rezizter picture rezizter · Mar 14, 2013

Another option is to use find and then pass it through sed.

find /path/to/files -type f -exec sed -i 's/oldstring/new string/g' {} \;