Replace Text in Multiple Files (with sed)

From Birnam Designs Wiki

Jump to: navigation, search

In the bash command prompt, you can find and replace text in multiple files like this:

find . -name "*.php" -exec sed -i 's/old/new/g' {} \;

The find tool creates the list of files to search, and passes each one into sed with the -exec parameter.

sed uses the -i parameter to specify that it is acting on the same file it is receiving as input.

's/old/new/g' is standard regex syntax

{} tells sed to use the file passed by find

\; ends the sed command. The semicolon is escaped so that bash doesn't see it as the end of its command instead.

You can also Find and Replace text in multiple files (with Perl)

Share This!
This page was last modified on 11 February 2010, at 20:06. This page has been accessed 674 times.