Easy Search and Replace in Multiple Files on Linux Command Line

I recently came across a typo that existed in a bunch of html files on my web server. I thought it should be easy enough to change, but since it was in a number of files, editing it by hand would be time consuming. Fortunately, there is an easy, one liner command to replace the text in multiple files in a sub directory using recursion.

grep -lr -e '<oldword>' * | xargs sed -i 's/<oldword>/<newword>/g'

This command broken down:

  • grep for the word in a files, use recursion (to find files in sub directories), and list only file matches
  • | xargs passes the results from the grep command to sed
  • sed -i uses a regular expression (regex) to evaluate the change: s (search) / search word / target word / g (global replace)

For more information, see man pages for grep, sed, and xarg. Also it is very handy to learn about regular expressions as they are a valuable tool to any command line programmer!

Update 2009/7/19:

Thanks to reader btr we have a great one-line perl command that will perform the same task:

Perl provides a really nice one-line for this kind of thing:

perl -p -i -e ’s///g’ *

It also provides the option of creating a backup of each file changed:

perl -p -i.bak -e ’s///g’ *

mnemonic: PIE (”easy as pie”, etc.)
google “perl pie” and you’ll get lots of info for other uses of this technique.

http://www.linux.org/lessons/short/perlpie/perl_pie.html

Share and Enjoy:
  • Twitter
  • del.icio.us
  • Digg
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Print
  • StumbleUpon

4 Trackbacks

You can leave a trackback using this URL: http://systembash.com/content/easy-search-and-replace-in-multiple-files-on-linux-command-line/trackback/

  1. By Dave Drager on May 7, 2009 at 3:13 am

    New blog post: Easy Search and Replace in Multiple Files on Linux Command Line http://bit.ly/AR8vZ

  2. By tom howard on May 7, 2009 at 3:57 am

    RT @ddrager New blog post: Easy Search and Replace in Multiple Files on Linux Command Line http://bit.ly/AR8vZ

  3. By Dave Drager on July 20, 2009 at 5:27 am

    Post Update: Single line search/replace in multiple files, 100% more perl! http://su.pr/1S9lWS

  4. By Matthew Lanham on March 13, 2010 at 10:29 pm

    Useful command line info for find & replace http://bit.ly/ddzeQo

4 Comments

  1. max

    Awesome! so helpful. thanks

    Posted June 23, 2009 at 4:54 pm | Permalink
  2. nobu

    Very useful, thks from Spain!!

    Posted July 3, 2009 at 6:16 am | Permalink
  3. btr

    Perl provides a really nice one-line for this kind of thing:

    perl -p -i -e ’s///g’ *

    It also provides the option of creating a backup of each file changed:

    perl -p -i.bak -e ’s///g’ *

    mnemonic: PIE (“easy as pie”, etc.)
    google “perl pie” and you’ll get lots of info for other uses of this technique.

    http://www.linux.org/lessons/short/perlpie/perl_pie.html

    Posted July 20, 2009 at 1:04 am | Permalink
  4. @btr: That is extremely helpful, thank you! Will update post.

    Posted July 20, 2009 at 1:18 am | Permalink

Post a Comment

Your email is never shared. Required fields are marked *

*
*