Thursday, February 27, 2014

Linux : Replace string in multiple files recursively

Hi,

If you want to search and replace specific string from multiple file recursively then below command is useful  for it :

Linux Command:
# grep -rl "string1" * -R | xargs sed -i 's/string1/string2/g'


Here above command is combination of  'grep' and 'sed', 
grep : Command will search for "string1" and returns file-name where string1 found

sed : will replace the "string1" to "string2"

string1 : old string
string2 : New string

You can watch the above process on youtube also...

http://youtu.be/GcopukZz3io

5 comments :

smokinXP said...

Good tip; thanks

However, what if the string to be replaced contains a " ?

Something like this:
href="./someaddress.com_files

Tarun Kumar Singhal said...

Plz give me old and new string for replacement

Tarun Kumar Singhal said...

Plz give me old and new string for replacement

JuanDA said...

Hello, awsome.

I need to change the text "/cess/" to "/cessV2/" . what should i do?

Thanks

Unknown said...

replace / to |