Search and Replace string with SED

sed (Stream Editor) is a tiny cool tool shipped in most linux distros. most of the times I use sed to lookup for pieces of strings in my projects and replace them in cases that I’m moving them from one environment to another.

 to run sed for search and replace purpose on a single file you can do:

sed -i 's/oldstring/newstring/g' /some/path/file.txt

The above command will look up the file.txt in and search all occurances of ‘oldstring’ to ‘newstring’.

To do this in a recursive mode and all the files in a directory you can mix it up with find command.

find . -type f -exec sed -i 's/oldstring/newstring/g' {} \;

note I’ve passed “.” as the target directory to find, means I should be in the directory that I wish to run the search in. you can also replace “.” with the path of the directory you wish to do your search and replace in.

Leave a comment

Leave a Reply