{"id":7671,"date":"2018-07-09T11:45:55","date_gmt":"2018-07-09T03:45:55","guid":{"rendered":"http:\/\/rmohan.com\/?p=7671"},"modified":"2018-07-09T11:45:55","modified_gmt":"2018-07-09T03:45:55","slug":"replacing-ip-address-in-apache2-config-files-with-sed","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7671","title":{"rendered":"Replacing IP Address in Apache2 config files with SED"},"content":{"rendered":"<p>Suppose i just mirrored my vps machine (starting from a clone and then rsync-ing all needed files) with <strong>rsync<\/strong>. Obviously i need to change the IP Address value contained into all the config files, but I\u2019m lazy.<br \/>\nSo, let\u2019s use \u201cSED\u201d to do it at once, with a single line command.<br \/>\nI need to replace the IP Address \u201c192.168.100.5\u201d with \u201c192.168.100.4\u201d in all files contained in \/etc\/apache2\/*<br \/>\n<span id=\"more-1106\"><\/span><br \/>\nOur command for one file should be:<\/p>\n<pre>$ sed -i 's\/192.168.100.5\/192.168.100.4\/g' \/etc\/apache2\/sites-available\/default\r\n<\/pre>\n<p>We want to do it on a bounce of files that contain that string, but unfortunately SED can\u2019t accept wildcard chars so we need run it through a loop.<br \/>\nFor this purpose we can use the linux <strong>FIND<\/strong> utility so we will end up with sed within this loop. The command should look like this:<\/p>\n<pre>$ find \/etc\/apache2\/sites-available\/ -type f -exec sed -i 's\/192\\.168\\.100\\.4\/192\\.168\\.100\\.5\/g' {} \\;\r\n<\/pre>\n<p>And while we are here, let\u2019s say that if you manage to obtain the backup machine from a clone of the production machine and you are mantaining the filesystem in sync, this is what you should do with relevant files once you have finished to sync.<\/p>\n<p>&nbsp;<\/p>\n<p>sed -i -r &#8216;s\/192.168.1.35$\/192.168.1.14\/g&#8217;<\/p>\n<p>find . -type f -exec sed -i &#8216;s\/192\\.168\\.1\\.35\/192\\.168\\.1\\.14\/g&#8217; {} \\;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Suppose i just mirrored my vps machine (starting from a clone and then rsync-ing all needed files) with rsync. Obviously i need to change the IP Address value contained into all the config files, but I\u2019m lazy. So, let\u2019s use \u201cSED\u201d to do it at once, with a single line command. I need to replace [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7671"}],"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=7671"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7671\/revisions"}],"predecessor-version":[{"id":7672,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7671\/revisions\/7672"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}