Search and Replace Text Recursively Linux Command Line

This one just for my note, a simple linux command line to search and replace text and code within files recursively. Simple, just oneliner, but this oneliner really help me much.

Using combination of Grep and Sed command, the following linux command line will search for ‘http://abc.wordpress.com’ code and replace it with ‘http://abcd.wordpress.com’ (without single quote).

grep -lr -e 'http://abc.wordpress.com/' * | xargs sed -i 's/http:\/\/abc\.wordpress\.com/http:\/\/abcd\.wordpress\.com/g'

Backslashes should be added before all special characters like . and / in the Sed parameters. The general format is like this:

grep -lr -e 'search' * | xargs sed -i 's/search/replace/g'

Leave a Reply