Find and Replace Across Multiple Files

SEPTEMBER 22, 2006

Archiving for my own benefit the results of yet another 5 minute look for how to do find and replace across multiple files from the command line:

  1. Use sed:

       sed -i 's/foo/foo_bar/g'  *.html
    
  2. use the old perl hack:

       perl -w -pi~ -e 's/foo/bar/' [files]
    

    Notes: -p: loop, -i edit files in place (backup with extension if supplied), -w enable warnings

  3. Install rpl

Combining either (1) or (2) with find is pretty powerful. E.g. to do a find and replace on all html files in all subdirectories:

     perl -w -pi -e 's/foo/bar/' `find <path> -name '*.html'`