Search and replace in lots of files with find and sed

Written by
Date: 2012-12-05 17:51:32 00:00

Bash on Linux or Mac OS X, is full of tools, and you can perform a lot of repetitive tasks with a single line.

One that I like a lot, is to change a string in a lot of files at the same time, with just one line of code.

You will need find and sed.

find /home/user/ -type f -exec sed -i 's/old-string/new-string/g' {} \;

Replace:

  • /home/user/: With the path where your files are.
  • old-string: With the string you need to look for.
  • new-string: With the string you want to be in place of old-string

Enjoy the power of Bash.