How to replace a string in multiple files in linux command line

Solution 1:

cd /path/to/your/folder
sed -i 's/foo/bar/g' *

Occurrences of "foo" will be replaced with "bar".

On BSD systems like macOS, you need to provide a backup extension like -i '.bak' or else "risk corruption or partial content" per the manpage.

cd /path/to/your/folder
sed -i '.bak' 's/foo/bar/g' *

Solution 2:

Similar to Kaspar's answer but with the g flag to replace all the occurrences on a line.

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

For global case insensitive:

find ./ -type f -exec sed -i 's/string1/string2/gI' {} \;