Find and replace text in many files with sed

Rename a function across a project from the terminal.

SNP 071 #426 Filed cmd linux

shell
grep -rl 'oldName' src | xargs sed -i 's/oldName/newName/g'

grep -rl lists the files that contain the text, sed -i edits them in place. On macOS sed -i needs an argument; use sed -i ''.

Use a different delimiter when the text has slashes in it, any character works:

shell
sed -i 's#/var/www/old#/var/www/new#g' /etc/nginx/sites-available/*

Run the grep on its own first to see which files you're about to touch. And commit before you start, so git diff shows what changed and git checkout . undoes it.