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

10

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  #

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

8/16/2008
6/22/2011
12/14/2013
6/16/2014

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

4/20/2009

gad @ 3:08 am #

----

9/5/2009

Afiyf @ 6:05 pm #

----

4/28/2010

Jabba @ 6:41 pm #

----

8/14/2010

pi4a @ 9:21 am #

----

10/5/2010

Ankur Vaish @ 1:42 am #

----

1/25/2011

Alvin Mites @ 3:15 pm #

----

Leave a Comment

Fields marked by an asterisk (*) are required.


one + 1 =

Switch to our mobile site