Replace Text in Files Recursively (Linux)

To replace text in all the files in a directory, recursively, you can use grep.

sudo grep -rl texttoreplace /somedirectory/ | sudo xargs sed -i โ€˜s/repacethis/withthis/gโ€™

[bash]sudo grep -rl oldtext /example/directory/ | sudo xargs sed -i โ€˜s/oldtext/newtext/g'[/bash]

The g on the end, lets it replace all the instances of the text in each file.

More ubuntu and linux tips and code samples