{"id":1730,"date":"2016-04-28T15:32:53","date_gmt":"2016-04-28T23:32:53","guid":{"rendered":"http:\/\/systemsolver.com\/StatlerBlog\/?p=1730"},"modified":"2016-04-28T15:32:53","modified_gmt":"2016-04-28T23:32:53","slug":"linux-command-line-search-and-replace","status":"publish","type":"post","link":"https:\/\/systemsolver.goodhealthyday.com\/StatlerBlog\/2016\/04\/28\/linux-command-line-search-and-replace\/","title":{"rendered":"linux command line search and replace"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>In linux command line, search recursively through directories, looking in all files for a particular string, and replace that string with something else:<\/p>\n<p style=\"background-color: silver; padding: 20px;\"><code>find .\/ -type f -exec sed -i 's\/string1\/string2\/' {} \\;<\/code><\/p>\n<p>Where string1 is the search string and string2 is the replace string.<\/p>\n<p>Examples that replace a user name within all sub-folders of the\u00a0 home directory (to aid in copying a user&#8217;s settings)<\/p>\n<div style=\"background-color: silver; padding: 20px;\"><code>find \/home\/tech\/ -name \".*\" -type f -exec sed -i 's\/tech\/user\/' {} \\;<\/code><\/div>\n<p>For replacement strings that include special characters like forward and back slashes:<\/p>\n<div style=\"background-color: silver; padding: 20px;\"><code>find .\/ -type f -exec sed -i 's|F:\\\\Home\\\\Libre\\\\MACROshortcut|\\\/mnt\\\/local-files\\\/Libre\\MACROsymlink|g' {} \\;<\/code><\/div>\n<p>For a bash script maybe:<\/p>\n<div style=\"background-color: silver; padding: 20px;\"><code>#!\/bin\/bash<br \/>\nSTRING1=tech<br \/>\nSTRING2=user<br \/>\nfind \/home\/tech\/ -type f -exec sed -i 's\/$STRING1\/$STRING2\/' {} \\;<\/code><\/div>\n<p>And to run the bash script asking for input:<\/p>\n<div style=\"background-color: silver; padding: 20px;\"><code>#!\/bin\/bash<br \/>\nSTRING1=tech<br \/>\necho -n \"What is the new user name?\"<br \/>\nread STRING2<br \/>\nfind \/home\/tech\/ -type f -exec sed -i 's\/$STRING1\/$STRING2\/' {} \\; echo \"&lt;$STRING1&gt; has been replaced with &lt;$STRING2&gt;\"<\/code><\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; In linux command line, search recursively through directories, looking in all files for a particular string, and replace that string with something else: find .\/ -type f -exec sed -i &#8216;s\/string1\/string2\/&#8217; {} \\; Where string1 is the search string and string2 is the replace string. Examples that replace a user name within all sub-folders [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1730","post","type-post","status-publish","format-standard","hentry","category-general"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/systemsolver.goodhealthyday.com\/StatlerBlog\/wp-json\/wp\/v2\/posts\/1730","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/systemsolver.goodhealthyday.com\/StatlerBlog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/systemsolver.goodhealthyday.com\/StatlerBlog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/systemsolver.goodhealthyday.com\/StatlerBlog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/systemsolver.goodhealthyday.com\/StatlerBlog\/wp-json\/wp\/v2\/comments?post=1730"}],"version-history":[{"count":0,"href":"https:\/\/systemsolver.goodhealthyday.com\/StatlerBlog\/wp-json\/wp\/v2\/posts\/1730\/revisions"}],"wp:attachment":[{"href":"https:\/\/systemsolver.goodhealthyday.com\/StatlerBlog\/wp-json\/wp\/v2\/media?parent=1730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/systemsolver.goodhealthyday.com\/StatlerBlog\/wp-json\/wp\/v2\/categories?post=1730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/systemsolver.goodhealthyday.com\/StatlerBlog\/wp-json\/wp\/v2\/tags?post=1730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}