{"id":3233,"date":"2014-06-21T08:52:56","date_gmt":"2014-06-21T00:52:56","guid":{"rendered":"http:\/\/rmohan.com\/?p=3233"},"modified":"2014-06-21T08:52:56","modified_gmt":"2014-06-21T00:52:56","slug":"check-if-folder-exists","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3233","title":{"rendered":"check if folder exists"},"content":{"rendered":"<p>Bash Script check if folder exists<\/p>\n<div class=\"post-header\"><\/div>\n<div id=\"post-body-5931489096580547611\" class=\"post-body entry-content\">\n<div dir=\"ltr\" style=\"text-align: left;\">\nThis piece of snippet code below will check if a folder exists.<\/p>\n<p>Linux OS that has NTFS driver installed, can easily to plug in and plug out NTFS formatted external drives.<\/p>\n<p>Or look for ntfs-3g driver if NTFS is not supported.<\/p>\n<p>And can easily be used by both Windows OS and Linux OS.<\/p>\n<p>This piece of snippet code below will check if a folder exists and execute a command if a folder is found.<\/p>\n<p>In Linux world, depends on the distro you are using.<\/p>\n<p>If you insert an NTFS formatted external drive you need to mount the drive.<\/p>\n<p>Once the external drive is inserted type: df -h<\/p>\n<p>Then you will have this output or it might be different in your distro.<\/p>\n<p>\/dev\/sdb1 \u00a0 1.9T<\/p>\n<p>Then you can proceed to mount the drive like:<\/p>\n<p>mount -t ntfs \/dev\/sdb1 \/media\/Week1 \u00a0(Week1 is the folder name, can be change to any name)<\/p>\n<p>Of course there are other methods, if the command above will not work.<\/p>\n<p>Type &#8220;man mount&#8221; (don&#8217;t include quotes when typing on the terminal) if you want to dig further about mount command.<\/p>\n<p>For example if you have four drives for Week1 to Week4 rotational backup (monthly backup one drive for each week).<\/p>\n<p>For readability and clarity purposes, its better to have one folder specific for each week.<\/p>\n<p>So in \/media or whichever folder you mount the external drives. There will be Week1, Week2, Week3 or Week4 folders.<\/p>\n<p>Of course backup can be done manually, but it will be awesome and if it could be done automatically.<\/p>\n<p>Bash script and crontab will come to the rescue.<\/p>\n<p>For Bash script to check which folder to use, this piece of snippet code below.<\/p>\n<p>If there are four folders on \/media and each external drive is mounted to an specific folder.<\/p>\n<p>Once the drive is inserted, Linux OS will automatically activate the mount drive and the mounted folder.<\/p>\n<p>Here&#8217;s the piece of snippet code below:<\/p>\n<p>======================<\/p>\n<p><span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">#!\/bin\/sh<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\"><br \/>\n<\/span> <span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\"># Path to the mounted folders<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">W1=&#8221;\/media\/Week1&#8243;<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">W2=&#8221;\/media\/Week2&#8243;<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">W3=&#8221;\/media\/Week3&#8243;<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">W4=&#8221;\/media\/Week4&#8243;<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\"><br \/>\n<\/span> <span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\"># Check if folder exists<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">if [ -d &#8220;$W1&#8221; ]<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">then<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">\u00a0PathBackup=&#8221;$W1&#8243;<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\"><br \/>\n<\/span> <span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">elif [ -d &#8220;$W2&#8221; ]<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">then<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">PathBackup=&#8221;$W2&#8243;<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\"><br \/>\n<\/span> <span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">elif [ -d &#8220;$W3&#8221; ]<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">then<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">PathBackup=&#8221;$W3&#8243;<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\"><br \/>\n<\/span> <span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">elif [ -d &#8220;$W4&#8221; ]<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">then<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">PathBackup=&#8221;$W4&#8243;<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\"><br \/>\n<\/span> <span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">fi<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\"><br \/>\n<\/span> <span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\"># you can do a simple cp or copy command<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\"># or any command that you want to run<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\"># command below uses rysnc and pipe to gzip and date as filename<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\"># the date used is the date that the script runs<\/span><br \/>\n<span style=\"font-family: Verdana, sans-serif; background-color: #cfe2f3;\">rsync \u00a0-arv \/home\/backup\/store\/ | gzip &gt; &#8216;$PathBackup&#8217; `date &#8216;+%m-%d-%Y&#8217;`.zip<\/span><\/p>\n<p>======================<\/p>\n<p>And you can configure crontab to run the script at a specified date and time.<\/p>\n<p>Type crontab -e and configure the settings as desired.<\/p><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bash Script check if folder exists<\/p>\n<p> This piece of snippet code below will check if a folder exists.<\/p>\n<p>Linux OS that has NTFS driver installed, can easily to plug in and plug out NTFS formatted external drives.<\/p>\n<p>Or look for ntfs-3g driver if NTFS is not supported.<\/p>\n<p>And can easily be used by both Windows [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3233"}],"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=3233"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3233\/revisions"}],"predecessor-version":[{"id":3234,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3233\/revisions\/3234"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}