5/4/2006

Search and Replace in all files within a directory recursively on Linux

Man…this was difficult.

I have a directory on my computer (I run linux for my desktop) that has a bunch of directories in it. I needed to change one string in each of the files to be something else.

I did this like 2 months ago but this time I had a really hard time finding the solution. I also remember that last time I tried like 6 different commands and none of them worked.

So, to search recursively through directories, looking in all the files for a particular string, and to replace that string with something else (on linux), this command should work:

find ./ -type f -exec sed -i ’s/string1/string2/’ {} \;

Where string1 is the search string and string2 is the replace string.

Just for future reference

(also, it looks like my wordpress theme is stylizing those single quotes to look like something else. When I copied and pasted that onto the command line I got this error: sed: -e expression #1, char 1: unknown command: `
I fixed it by replacing those ` (backtick) characters with single quotes)

Note: Be sure to replace the ` (backtick) characters in all these commands with single quotes.

[tags]search and replace, linux search and replace, recursive search and replace, sed, xargs[/tags]

Filed under Reference by

Comment

Comments on Search and Replace in all files within a directory recursively on Linux »

5/4/2006

Richard K Miller @ 1:59 pm

Thanks for posting this. I’ve always wanted to learn sed better so I’m glad to have this for reference.

Stuart Jansen @ 3:03 pm

It would have been better to use xargs instead of -exec. Using xargs, you will fork fewer times. For large numbers of files, that means you will be done faster. In other words:

find ./ -type f | xargs sed -i ’s/string1/string2/’

Instead of editing all files in a directory, you can use the same concept with grep to edit only files containg a certain string. In other words:

grep -rl matchstring somedir/ | xargs sed -i ’s/string1/string2/’

Hmm, maybe I should post something on my own blog about the wonders of sed. More than once, sed has allowed me to do in minutes what would have literally taken days to do by hand.

7/28/2006

kosta @ 6:35 am

The trick works fine except for one thing: It only replaces the first instance of string1 it finds in each file. For example if you had 100 instances of string1 in some files, then you would have to run the command 100 times!

Also, if you don’t know how many times string1 can be found in the files, then you also don’t know how many times ou have to run the command before everything is replaced (unless you check by hand which I believe is not the point). It would be nice to have an output while the files are being treated…

Any ideas?

Kosta

8/10/2006

Scott @ 11:02 am

grep -rl matchstring somedir/ | xargs sed -i ’s/string1/string2/g’

the g on the edn will replace globally - all instances of string1

Scott

8/24/2006

Jonathan Lumb @ 3:31 pm

I have discovered another way of doing this that is documented on my Blog here:
http://www.sprayfly.com/2006/08/unixlinux_multiple_file_find_a.php
It was actually quite hard to find documentation for such a useful feature on Google!
Jono

9/27/2006

Marc A. Brown @ 6:02 am

It’s much more simpler to use the ‘rpl’ command, as shown below:

rpl -x’.cpp’ -x’.h’ -pR “old-string” “new-string” *

Here, all files with a .cpp or .h suffix wil be searched for
an “old-string”. If found the “old-string” is replaced by the
“new-string” in all directories recursively.
To install on Ubuntu or Debian just do ‘apt-get install rpl’.

KR

12/6/2007

PST @ 8:33 pm

Excellent help….

Leave a Comment