Code | Open Source | Web

Search and Replace Commands in Linux

Sed command does a great job for search and replace, let’s see an example:

sed -i -- 's/foo/bar/g' myfile.txt

This will convert the string “foo” to “bar” in myfile.txt, if you would like to perform this on all files use:

sed -i -- 's/foo/bar/g' *

The same can be done recursively using find command:

find . -type f -exec sed -i 's/foo/bar/g' {} +