BlogFind and change words in files - Linux (and Mac)

30th July 2013

Here are two useful little Bash commands to allow you to check for a phrase and change a phrase in all files inside a directory.

I've found these particularly useful when moving databases between cheap hosting packages where you don't have access to choose the name of the database.

  • Check for the phrase, this will list all files containing the phrase...
grep "old_db_name" *.sql
  • Make the change...
perl -pi -w -e 's/old_db_name/new_db_name/g;' *.sql

Let's Talk