How to find and replace a word recursively ?

Here in this post we will see general command how to find and replace a word across multiple files in a directory and recursively with in the directory .

Firstly we need to know what is the keyword that we would like to replace , what is the new keyword that will replace old keyword and the Base directory under which we should look at the files recursively

1.Old Keyword

2.New keyword

3.Base directory

Lets see the command to find the Old keyword existence of files

grep -r old_keyword *

further we can use the same command to exclude some of the directories if we don’t want to replace files in side it

grep -r old_keyword * --exclude-dir={dir1,dir2,dir3}

Now let us use above search command in combination of the sed to replace find old_keyword with new_keyword .

grep -r old_keyword * --exclude-dir={dir1,dir2,dir3} | xargs sed -I 's/old_keyword/new_keyword/g'

lets check it out …

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *