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

153

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
1/7/2010
6/5/2011
6/22/2011
2/2/2012
2/3/2012
2/4/2012
2/5/2012
2/6/2012
6/4/2013
12/14/2013

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

5/4/2006

----

Stuart Jansen @ 3:03 pm #

----

7/28/2006

kosta @ 6:35 am #

----

8/10/2006

Scott @ 5:02 am #

----

9/27/2006

Marc A. Brown @ 12:02 am #

----

12/6/2007

PST @ 8:33 pm #

----

3/2/2009

JIA pei @ 3:42 pm #

----

3/12/2009

Sam @ 10:24 pm #

----

4/20/2009

gad @ 3:08 am #

----

5/1/2009

Vishwa @ 7:28 am #

----

6/5/2009

Andrew P. @ 12:59 pm #

----

9/5/2009

Afiyf @ 6:05 pm #

----

4/28/2010

Jabba @ 6:41 pm #

----

8/6/2010

puzo @ 9:01 am #

----

puzo @ 9:03 am #

----

8/14/2010

pi4a @ 9:21 am #

----

10/5/2010

Ankur Vaish @ 1:42 am #

----

11/5/2010

Lamnk @ 7:58 pm #

----

1/25/2011

Alvin Mites @ 3:15 pm #

----

2/22/2011

Zach L @ 8:10 am #

----

3/24/2011

Diego Vieira @ 6:45 am #

----

8/12/2011

Gwyneth Llewelyn @ 12:35 am #

----

9/4/2011

@gearond @ 10:45 pm #

----

12/5/2011

sameer @ 8:02 am #

----

1/16/2012

allnudecelebsy @ 1:01 pm #

----

1/18/2012

Allegro @ 8:35 am #

----

Ida @ 9:25 am #

----

1/22/2012

Eliza Correale @ 3:18 am #

----

1/23/2012

cpanel vps @ 3:35 am #

----

----

Omicierry @ 5:30 am #

----

1/24/2012

Casandra Konat @ 7:34 am #

----

1/25/2012

Janyce Rigel @ 10:13 am #

----

1/26/2012

----

----

1/27/2012

MonikaDrivesLE @ 2:53 am #

----

Johnie Deniro @ 10:48 am #

----

1/28/2012

Ashton Waldvogel @ 11:34 am #

----

----

1/30/2012

Elise Szalay @ 8:43 am #

----

----

red cumberbun @ 8:54 pm #

----

2/1/2012

----

efterskole @ 2:59 pm #

----

2/2/2012

efterskoleliv @ 9:57 am #

----

Oren Levecke @ 1:52 pm #

----

unmetered vps @ 2:21 pm #

----

----

a4830726 @ 10:50 pm #

----

Gun Games @ 11:29 pm #

----

2/3/2012

----

Mitch Newland @ 1:59 am #

----

Gun Games @ 2:28 am #

----

----

----

Andrew Pelt @ 9:46 am #

----

Dog Snuggie @ 10:57 am #

----

SEO Starter Pack @ 11:23 am #

----

mpc 3001 @ 2:12 pm #

----

----

----

pregnancy @ 4:25 pm #

----

----

boot camp @ 5:19 pm #

----

----

----

----

funny videos @ 8:35 pm #

----

----

2/4/2012

Karoline Maack @ 1:28 am #

----

----

----

nsa sex @ 10:06 am #

----

----

----

----

zwrot podatku @ 6:43 pm #

----

----

Nickolas @ 10:10 pm #

----

2/5/2012

zwrot podatku @ 12:19 am #

----

----

----

Eura Moffett @ 2:57 am #

----

Amado Battisti @ 3:49 am #

----

Dusti Kone @ 4:18 am #

----

Jesse Tsau @ 12:45 pm #

----

----

girokonto @ 2:40 pm #

----

----

a688275 @ 4:27 pm #

----

seo naples @ 4:33 pm #

----

balustrady @ 5:52 pm #

----

Arianna Valine @ 6:57 pm #

----

Musica giovani @ 8:02 pm #

----

----

lit parapluie @ 9:01 pm #

----

Steve @ 10:47 pm #

----

----

----

Al Labar @ 11:50 pm #

----

----

2/6/2012

----

----

Jami Zampieri @ 2:09 am #

----

cliquez ici @ 2:24 am #

----

----

nakede @ 5:11 am #

----

----

----

sorbetiere @ 7:26 am #

----

Jesse Bagwell @ 8:02 am #

----

----

Harriett Soo @ 9:11 am #

----

----

Terrell Wies @ 9:26 am #

----

Fermin Dartt @ 9:44 am #

----

sex artikelen @ 11:06 am #

----

internet dating @ 12:02 pm #

----

2/20/2012

sameer @ 8:00 am #

----

6/1/2012

Scott Fleming @ 10:41 am #

----

6/11/2012

Raj @ 3:55 am #

----

2/5/2013

UX1vchw @ 7:45 pm #

----

2/27/2013

Blatte @ 7:15 am #

----

Leave a Comment

Fields marked by an asterisk (*) are required.


9 × = nine

Register Login

Switch to our mobile site