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

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

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]

154 comments on this post.
  1. Richard K Miller:

    ----

  2. Stuart Jansen:

    ----

  3. kosta:

    ----

  4. Scott:

    ----

  5. Marc A. Brown:

    ----

  6. PST:

    ----

  7. Useful find Commands | unsharpTech:

    ----

  8. JIA pei:

    ----

  9. Sam:

    ----

  10. gad:

    ----

  11. Vishwa:

    ----

  12. Andrew P.:

    ----

  13. Afiyf:

    ----

  14. Batch Find and Replace with the Linux Command Line « The Wubbulous World of Jerry Waller:

    ----

  15. Jabba:

    ----

  16. puzo:

    ----

  17. puzo:

    ----

  18. pi4a:

    ----

  19. Ankur Vaish:

    ----

  20. Lamnk:

    ----

  21. Alvin Mites:

    ----

  22. Zach L:

    ----

  23. Diego Vieira:

    ----

  24. To replace a string in all files in linux:

    ----

  25. Recursive Search and Replace in *Nix « orz:

    ----

  26. Gwyneth Llewelyn:

    ----

  27. @gearond:

    ----

  28. sameer:

    ----

  29. allnudecelebsy:

    ----

  30. Allegro:

    ----

  31. Ida:

    ----

  32. Eliza Correale:

    ----

  33. cpanel vps:

    ----

  34. Cathey Pfeuffer:

    ----

  35. Omicierry:

    ----

  36. Casandra Konat:

    ----

  37. Janyce Rigel:

    ----

  38. Malia Eisenhaver:

    ----

  39. invest liberty reserve:

    ----

  40. MonikaDrivesLE:

    ----

  41. Johnie Deniro:

    ----

  42. Ashton Waldvogel:

    ----

  43. liberty reserve:

    ----

  44. Elise Szalay:

    ----

  45. Android Free Application:

    ----

  46. Android Free Application:

    ----

  47. invest liberty reserve:

    ----

  48. red cumberbun:

    ----

  49. invest liberty reserve:

    ----

  50. efterskole:

    ----

  51. efterskoleliv:

    ----

  52. Oren Levecke:

    ----

  53. unmetered vps:

    ----

  54. Beverlee Keezer:

    ----

  55. a4830726:

    ----

  56. I've said that least 4830726 times:

    ----

  57. Gun Games:

    ----

  58. Tabatha Stancer:

    ----

  59. Mitch Newland:

    ----

  60. Gun Games:

    ----

  61. Denver TV Repair:

    ----

  62. solicitors crewe:

    ----

  63. Andrew Pelt:

    ----

  64. Dog Snuggie:

    ----

  65. SEO Starter Pack:

    ----

  66. mpc 3001:

    ----

  67. Cheap Bike Clothing:

    ----

  68. Przewody hydrauliczne:

    ----

  69. pregnancy:

    ----

  70. fire investigator:

    ----

  71. Fun & Games:

    ----

  72. boot camp:

    ----

  73. travertine tiles cheap:

    ----

  74. unique weddings:

    ----

  75. Philadelphia Personal Injury Lawyer:

    ----

  76. intimate weddings:

    ----

  77. funny videos:

    ----

  78. Personal Injury Lawyers in Philadelphia:

    ----

  79. Free Online Games:

    ----

  80. reproduction paintings:

    ----

  81. Foreclosure Homes Sanford:

    ----

  82. Karoline Maack:

    ----

  83. Clemencia Wohlert:

    ----

  84. baccarat online:

    ----

  85. zwrot podatku z holandii:

    ----

  86. Gamer:

    ----

  87. nsa sex:

    ----

  88. billiga webbhotell:

    ----

  89. LittleBlueKangaroo:

    ----

  90. samsung 1080p hdtv:

    ----

  91. Further Games:

    ----

  92. zwrot podatku:

    ----

  93. zwrot podatku holandia:

    ----

  94. Nickolas:

    ----

  95. zwrot podatku z holandii:

    ----

  96. zwrot podatku:

    ----

  97. AIR DOCTOR INC.:

    ----

  98. zwrot podatku holandia:

    ----

  99. Eura Moffett:

    ----

  100. Amado Battisti:

    ----

  101. Dusti Kone:

    ----

  102. Gaming Fun:

    ----

  103. Jesse Tsau:

    ----

  104. web link directory:

    ----

  105. girokonto:

    ----

  106. odchudzanie tabletki:

    ----

  107. a688275:

    ----

  108. seo naples:

    ----

  109. balustrady:

    ----

  110. Arianna Valine:

    ----

  111. Web Game:

    ----

  112. Aqua-Comfort Wasserbetten:

    ----

  113. Musica giovani:

    ----

  114. Wart Treatments:

    ----

  115. lit parapluie:

    ----

  116. Steve:

    ----

  117. Find a Financial Advisor:

    ----

  118. Solar for my home:

    ----

  119. sex shop amsterdam:

    ----

  120. Al Labar:

    ----

  121. MLM Software Solutions:

    ----

  122. Latosha Lowrimore:

    ----

  123. imperial stock bricks:

    ----

  124. Jami Zampieri:

    ----

  125. cliquez ici:

    ----

  126. fernseher kaufen günstig:

    ----

  127. Web Game:

    ----

  128. plancha electrique:

    ----

  129. nakede:

    ----

  130. odchudzanie tabletki:

    ----

  131. InterQuotes.co.uk:

    ----

  132. sorbetiere:

    ----

  133. cheap car insurance policy:

    ----

  134. Find a Financial Advisor:

    ----

  135. Jesse Bagwell:

    ----

  136. clip on chandelier:

    ----

  137. disque dur interne 2.5:

    ----

  138. Harriett Soo:

    ----

  139. in car cameras:

    ----

  140. odchudzanie tabletki:

    ----

  141. Terrell Wies:

    ----

  142. Fermin Dartt:

    ----

  143. sex artikelen:

    ----

  144. sittingbourne electricians:

    ----

  145. internet dating:

    ----

  146. casque audio tv sans fil:

    ----

  147. sameer:

    ----

  148. Scott Fleming:

    ----

  149. Raj:

    ----

  150. UX1vchw:

    ----

  151. Blatte:

    ----

  152. Search and Replace in all files within a directory recursively on Linux - ?????:

    ----

  153. Search and replace all files recursively | Nesity:

    ----

  154. Search and Replace in all files within a directory recursively on Linux | CODE Paint:

    ----

Leave a comment






− five = 2