Linux: Searching and Replacing string in multiple Files in a directory/ folder

I recently stuck into a condition in which I have to replace every occurrence of a certain word / string in a number of files spanning multiple directories, and this is the easiest way I found to do it. It uses grep to search for a certain word and if it find its it runs sed to replace the strings you want. Note: This will not work on windows systems

Basic Format

grep -rl 'stringToFind' /path/to/directory/where/files/are/saved | xargs sed -i 's/stringToFind/stringToReplace/g'
Note: The forward slash '/' delimiter in the sed argument could also be a different delimiter (pipe ‘|’ character). The pipe delimiter might be useful when searching through a lot of html files if you didn’t want to escape the forward slash, for instance.


grep -rl 'room' ./ | xargs sed -i 's/room/house/g'

This will search for the string ‘room’ in all files relative to the current directory and replace ‘room’ with ‘house’ for each occurrence of the string in each file.