{"id":37,"date":"2007-02-01T19:18:38","date_gmt":"2007-02-02T00:18:38","guid":{"rendered":"http:\/\/www.summet.com\/blog\/2007\/02\/01\/recursive-search-and-replace\/"},"modified":"2007-02-04T14:54:37","modified_gmt":"2007-02-04T19:54:37","slug":"recursive-search-and-replace","status":"publish","type":"post","link":"https:\/\/www.summet.com\/blog\/2007\/02\/01\/recursive-search-and-replace\/","title":{"rendered":"Search and Replace a string in many files recursively on unix"},"content":{"rendered":"<p><strong> <\/p>\n<pre>find \/net\/hc287\/summetj\/research\/dist1 -name &quot;Root&quot;<\/pre>\n<pre>-exec perl -pi.bak -e&#39;s\/summetj\\@flux.cc.gatech.edu:\/summetj\\@wilks1.cc.gatech.edu:\/g&#39; {} \\;<\/pre>\n<p> <\/strong><\/p>\n<p>The above command line will find all files named &quot;Root&quot; recursively under the&nbsp; \/net\/hc287\/summetj\/research\/dist1 direcotry and execute perl to replace the string &quot;summetj\\@flux.cc.gatech.edu:&quot; with the string &quot;summetj\\@wilks1.cc.gatech.edu:&quot;&nbsp; It also keeps a backup of all the changed files as Root.bak, just in case&#8230;. <\/p>\n<p>The {} is replaced by the find command with the name of the file, while the \\; tells find&#39;s -exec command where the program arguments end. The @ signs are escaped by \\&#39;s.&nbsp; This is useful if you need to search and replace a string in many files in a directory tree to update some settings, for example, if the CVSROOT machine has been changed from flux to wilks1 and you don&#39;t want to do a full new checkout of 45MB over a slow link.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>find \/net\/hc287\/summetj\/research\/dist1 -name &quot;Root&quot; -exec perl -pi.bak -e&#39;s\/summetj\\@flux.cc.gatech.edu:\/summetj\\@wilks1.cc.gatech.edu:\/g&#39; {} \\; The above command line will find all files named &quot;Root&quot; recursively under the&nbsp; \/net\/hc287\/summetj\/research\/dist1 direcotry and execute perl to replace the string &quot;summetj\\@flux.cc.gatech.edu:&quot; with the string &quot;summetj\\@wilks1.cc.gatech.edu:&quot;&nbsp; It also keeps &hellip; <a href=\"https:\/\/www.summet.com\/blog\/2007\/02\/01\/recursive-search-and-replace\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/comments?post=37"}],"version-history":[{"count":0,"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/posts\/37\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/media?parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/categories?post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.summet.com\/blog\/wp-json\/wp\/v2\/tags?post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}