{"id":662,"date":"2012-07-10T16:59:04","date_gmt":"2012-07-10T08:59:04","guid":{"rendered":"http:\/\/rmohan.com\/?p=662"},"modified":"2012-07-10T16:59:04","modified_gmt":"2012-07-10T08:59:04","slug":"search-and-replace-recursively-on-a-directory-in-linux","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=662","title":{"rendered":"Search and replace recursively on a directory in Linux"},"content":{"rendered":"<h3><strong>Search and replace recursively on a directory in Linux<\/strong><\/h3>\n<p>ere is the small bash shell script to make life simple&#8230; This script can do a search for string and replace with a new string recursively in a directory.<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n#!\/bin\/bash<br \/>\n# This script will search and replace all regular files for a string<br \/>\n# supplied by the user and replace it with another string.<br \/>\nfunction usage {<br \/>\necho &#8220;&#8221;<br \/>\necho &#8220;Search\/replace script&#8221;<br \/>\necho &#8220;Usage: .\/$0 searchstring replacestring&#8221;<br \/>\necho &#8220;Remember to escape any special characters in the searchstring or the replacestring&#8221;<br \/>\necho &#8220;&#8221;<br \/>\n}<\/p>\n<p>#check for required parameters<br \/>\nif [ ${#1} -gt 0 ] &amp;&amp; [ ${#2} -gt 0 ];<br \/>\nthen<\/p>\n<p>for f in `find -type f`;<br \/>\ndo<br \/>\ngrep -q $1 $f<br \/>\nif [ $? = 0 ];then<br \/>\ncp $f $f.1bak<br \/>\necho &#8220;The string $1 will be replaced with $2 in $f&#8221;<br \/>\nsed s\/$1\/$2\/g &lt; $f.1bak &gt; $f<br \/>\nrm $f.1bak<br \/>\nfi<br \/>\ndone<\/p>\n<p>else<br \/>\n#print usage informamtion<br \/>\nusage<br \/>\nfi<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Search and replace recursively on a directory in Linux <\/p>\n<p>ere is the small bash shell script to make life simple&#8230; This script can do a search for string and replace with a new string recursively in a directory.<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; #!\/bin\/bash # This script will search and replace all regular files for a string # supplied [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/662"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=662"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/662\/revisions"}],"predecessor-version":[{"id":663,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/662\/revisions\/663"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}