How to grep and replace How to grep and replace linux linux

How to grep and replace


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' {} \;


I got the answer.

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


You could even do it like this:

Example

grep -rl 'windows' ./ | xargs sed -i 's/windows/linux/g'

This will search for the string 'windows' in all files relative to the current directory and replace 'windows' with 'linux' for each occurrence of the string in each file.