Kim Spasaro

Recursively Find + Replace with sed and grep

April 3, 2017 ,

Have you ever needed to find and replace a word across multiple files in your project directory? Good news. You can recursively find-replace across all files (in all subdirectories) of your project by using sed and grep.

grep -lr -e "oldword" . | xargs sed -i "s/oldword/newword/g"

You can exclude directories from the command using the –exclude-dir=”.git” flag.

grep -lr --exclude-dir=".git" -e "oldword" . | xargs sed -i "s/oldword/newword/g"