Find & Replace across multiple files in linux

Here’s the post on my new blog – http://rushi.vishavadia.com/blog/find-replace-across-multiple-files-in-linux/
Below is an older version of the post:

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:

About these ads

58 thoughts on “Find & Replace across multiple files in linux

  1. Pingback: Find and replace multiple files | Carson Farmer

  2. Aaron

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

    Reply
    1. Shane

      gnu win utils – I install the lot on my machine. They go to %programfiles%/gnu/bin
      I then alter my path to include that in it.
      That gives me sed, grep, scp, ftp, ssh etc etc etc
      cygwin is cool but ends up putting an emulation type layer – gnu win utils are all precompiled for windows.

      Reply
  3. 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!!

    Reply
  4. umbug

    @ lm. The error probably lies in the text delimiter that you chose for sed.

    Sed gets confused when you use a “/” in the field that you want to replace letters. Try using something like :
    sed -i ‘s_2008_2009_g’ or sed -i ‘s:2008:2009:g’. That should help solve your problem.

    Reply
    1. Kushal Dave

      I still get the same error even after replacing it with “:” or “_” ..

      find . -name “*.gnu” -print | xargs sed -i ‘s:bottom:top:g’
      sed: 1: “./1LDAWC.gnu”: invalid command code .

      Reply
  5. Pingback: linux find and replaces in files « yada yada yada..

  6. Pingback: Buscar y reemplazar desde línea de comandos en linux « Código atado con alambre

  7. Pingback: Per Larsson (psld) 's status on Wednesday, 23-Sep-09 12:15:38 UTC - Identi.ca

  8. Strap

    You can use also this:

    ~$ find . -name "*.php" -exec sed -i 's/foo/bar/g' {} \; 
    

    find is a powerful command! :)

    My two cents.

    Bye

    Reply
  9. quesder

    What if I want to replace the path string “/d1/d2″ to “/d1/d2-new/ “?

    Does the escape char ‘\’ work here?

    Reply
  10. Playriver

    Mind Formal,run research critical dress what transfer arrange before plastic dress appear heat of secretary long great guide finger game unless commit slightly county happen working across officer judge movement rise run bloody discipline respond approach vote machine intention potential my liability pub why early grant of charge elsewhere child accident properly illustrate reform tool recently afterwards air encourage suppose mention need good source benefit problem compare artist money just clean transfer general therefore visitor crisis writer ourselves lead all him proposal art communication mass arrive sky successful tree start life

    Reply
  11. Ben

    What if i want to get sub directories within the directory im running the command in?
    Getting
    xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

    Thanks!

    Reply
  12. kun's

    I am getting following error, how can i solve?
    sed -i “s/asdjfl;askdjfl;ajkdf/sdfalsdjfaskl;d/g” *.c
    sed: invalid option —

    Thanks in advance..

    Reply
    1. Rushi Post author

      kun’s — i don’t seem to get that error. What version of sed are you using ?

      Just make sure the quotes and dashes are the correct ASCII version and you aren’t copy/pasting the command from anywhere. Word processing docs like MS Word will use a different style of quotes and dashes.

      Reply
  13. Pingback: Deutsches Java Blog von Jörg Herbst » Blog Archive » Vorher und nachher – Suchen und Ersetzen in mehreren Dateien

  14. Pingback: alias to replace a text in file - Admins Goodies

  15. Flos Headford

    Very useful. Thank you. I have done a little reading on this.
    As some have discovered, allowing spaces in file and directory names is almost always a mistake.
    It could be made clearer that the s/ part of the command is the bit where the user chooses which character is going to signify the end of the searched-for text and the beginning of the replacement text. You can’t use a / in either of the strings. (If you do, sed thinks you’re sending too many parameters.) If you use s% instead of s/ you can use / but not % in the two strings.

    Reply
  16. ha1e

    fixing my error: ‘Privacy and Cooking Policy’ with ‘Privacy and Cookie Policy’

    Lovely stuff. Thanks

    Reply
  17. Vivek

    For mac users:

    find . -name “*.php” -print | xargs sed -i “” ‘s/foo/bar/g’

    Reply
  18. glenbot

    Just a little info when trying to find/replace on ALL files. Performing:

    find . | xargs sed -i ‘s/foo/bar/g’

    you will get:

    sed: couldn’t edit .: not a regular file

    You need to exclude . and directories if you want to find and replace on “everything” and not just *.php. A more stable version to find/replace in all files:

    find . -type f -not -name “.*” -print | xargs sed -i ‘s/foo/bar/g’

    Reply
  19. Amit Durge

    I want to remove the weird / special characters from the bunch of text files. Here i used the following command to remove the weird characters but it gives me the error.

    Command :

    1) grep -lri -e ââ¬Å temp.txt | xargs sed -i ‘ s_ââ¬Å_”_g’

    2) grep -lri -e ââ¬Å temp.txt | xargs sed -i ‘ s/ââ¬Å/”/g’

    3) grep -lri -e ââ¬Å temp.txt | xargs perl -piew ‘ s_ââ¬Å_”_g’

    4) grep -lri -e ââ¬Å temp.txt | xargs perl -piew ‘ s/ââ¬Å/”/g’

    Here I want to replace ââ¬Å this character with ” quote.

    These are the some Weird characters that I want to remove from the text files :

    – , — , ; , : , ! , ¡ , ¿ , · , ‚ , ‹ , › , » , @ , / , \[ , \]\\ ,
    \^ , \+ , \ , \$ , \s , & , # , % , †, ‡ , ` , ´ , ¯ , ˘ , ¨ , § , ¶ ,
    © , ® , ℠, ° , º , ∂ , ∆ , ∠, ∑ , ± , = , ≠, ¬ , \ , , ~ ,
    ∫ , € , ª , à , â , Ã… , ä , Æ , Ç , è , ë , ï¬ , fl , Æ’ , í , ì ,
    î , ï , ó , ò , ô , ö , õ , ø , œ , ß
    and so on.

    Please help me on this & Also suggest the command to remove the bunch of weird characters.

    Reply
    1. Shane

      sed ‘s/’`echo “\— `’”"‘

      Basically the command is
      sed -e ‘s/’$(echo “octal – value”)’/replacement_words/g’

      grepping the web also reveals for stripping chars, adding a replace in there might work.:
      perl -i.bak -pe ‘s/[^[:ascii:]]//g’
      or
      sed -i ‘s/[^[:print:]]//’ FILENAME

      lastly – there is an older command called unix2dos, that may do what you require, stripping out not ascii stuff but it wont do the replace.

      Reply
  20. Pingback: Linux: search and replace in files | Decidable Dreams

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s