How to "search and replace" many files?

I use the grep command to search for a string in many files. Is there something similar to "search and replace"?


You are looking for the sed command. For example, to replace the "dog" with "cat" in all text files in the current directory:

sed -i 's/dog/cat/' *.txt

sed comes to mind. Example:

sed s/cat/dog/ <input >output

This searches for cat in a line and puts dog on it's place in the file input and writes to file output.


sed is the right tool; but, as an alternative, you can even use ex commands in vim:

vim -c 'args <files> | argdo %s/cat/dog/g | x'

where <files> is the list of files or patterns in which you want to make the substitution.