Find & Replace across multiple files in linux

August 5, 2008

I was trying to find a solution todo a find & replace across multiple files which was purely command line based. There are plenty of scripts out there which will accomplish this but I needed a single line command. After some google searches and some experimentation I came up with this snippet.

find . -name "*.php" -print | xargs sed -i 's/foo/bar/g'

It looks a bit complicated but its quite simple. There are three components to the command:

  1. find . -name "*.php" -print – Find all files (recursively) which has “.php” in the file and print them out. This will give you output like this:
    ./file.php
    ./includes/test.php
    ./classes/class.php
  2. xargs- This command is used when you want to pass a lot of arguments to one command. xargs will combine the single line output of find and run commands with multiple
    arguments, multiple times if necessary to avoid the max chars per line limit. In this case we combine xargs with sed
  3. sed -i 's/foo/bar/g' – aka Stream Editor is a tool which should be in every sys admin’s toolkit.  In this case every occurence of “foor” is replaced by “bar” in all the files found using the “find” command. Sed simply parses input and applies certain text transformations to it. There’s a lot to say about sed, you can find more at this tutorial.

This pretty much covers the core of the find & replace command. You could also open up a particular folder in an IDE and use it’s find and replace feature. But find + sed is quite fast and powerful.

Resources:

  • http://rushi.wordpress.com/ cfarmer

    Nice how to!
    This really saved my ass! Keep it up.

    C

  • Pingback: Find and replace multiple files | Carson Farmer

  • devGOD

    thanks this helped me out a lot.

  • suganya

    Awesome!! with a single command and it satisfies find and to replace a text.

  • Aaron

    I miss linux :( I am stuck on a windows vista machine at work. Does anyone know how to do this on Windows?

  • http://rushi.wordpress.com/ Rushi

    Aaron you can install Cygwin – http://www.cygwin.com/ – if you like Linux and the command line, you will like Cygwin.

  • http://lucasmonaco.com lm

    when i do this:
    /www/htdocs/html/article> find . -name “*.html” -print | xargs sed -i ‘s/2008/2009/g’
    I get the error:
    sed: 1: “./article/study_finds_c …”: invalid command code .

    Any idea what this means??? thanks in advance!!

  • Pingback: Find & Replace across multiple files in linux « Rushi’s Ramblings

  • Vishnu

    this command breaks when there is a space in the filename

    pls help

    thanks.

  • Ian Stanway

    A great post, thank you. For others who may be interested, to search only the current folder i.e. no subfolders, use maxdepth to prevent any levels deeper than the current being checked i.e.
    find . -maxdepth 1 -name “*.php” -print | xargs sed -i ‘s/foo/bar/g’

    ~Cyrix

  • Arno

    The rrep program lets you replace strings in multiple files and also recursively in directories. It also supports regular expressions. The usage is similar to grep.
    Available in Debian, Ubuntu and at http://sourceforge.net/projects/rrep/

Previous post:

Next post: