Grep and replace string from source code using command in Linux

Recently when we migrated our old code in Android to API level 30 i.e. androidx , lots of headers files got changed, whereas all the other backword compatible code remained same.

So to accomplish this migration, we have to rename the header imports to androidx imports, but changing all those thousands of source files manually was simply not possible.

But this finding and replacing the string from source code without opening files can be done using single command in Linux terminal as,

$ cd your_source
$ find $PWD -type f -exec sed -i 's/old_string/new_string/g' {} \;

Once you run this, all occurrences of “old_string” will get replaced with “new_string” in your complete source code.

Leave a Comment