{"id":3337,"date":"2014-07-07T14:45:39","date_gmt":"2014-07-07T06:45:39","guid":{"rendered":"http:\/\/rmohan.com\/?p=3337"},"modified":"2014-07-07T14:45:39","modified_gmt":"2014-07-07T06:45:39","slug":"if-condition-to-check-file-or-directory-exists","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3337","title":{"rendered":"if condition to check file or directory exists"},"content":{"rendered":"<p>-e: Returns true value, if file exists<\/p>\n<p>-f: Return true value, if file exists and regular file<\/p>\n<p>-r: Return true value, if file exists and is readable<\/p>\n<p>-w: Return true value, if file exists and is writable<\/p>\n<p>-x: Return true value, if file exists and is executable<\/p>\n<p>-d: Return true value, if exists and is a directory<\/p>\n<p>File Testing<br \/>\n-b filename &#8211; Block special file<br \/>\n-c filename &#8211; Special character file<br \/>\n-d directoryname &#8211; Check for directory existence<br \/>\n-e filename &#8211; Check for file existence<br \/>\n-f filename &#8211; Check for regular file existence not a directory<br \/>\n-G filename &#8211; Check if file exists and is owned by effective group ID.<br \/>\n-g filename &#8211; true if file exists and is set-group-id.<br \/>\n-k filename &#8211; Sticky bit<br \/>\n-L filename &#8211; Symbolic link<br \/>\n-O filename &#8211; True if file exists and is owned by the effective user id.<br \/>\n-r filename &#8211; Check if file is a readable<br \/>\n-S filename &#8211; Check if file is socket<br \/>\n-s filename &#8211; Check if file is nonzero size<br \/>\n-u filename &#8211; Check if file set-user-id bit is set<br \/>\n-w filename &#8211; Check if file is writable<br \/>\n-x filename &#8211; Check if file is executable<\/p>\n<p>example script,<\/p>\n<p>if a file does not exist<\/p>\n<p>if [ ! -f \/tmp\/rmohan.txt ]; then<br \/>\n    echo &#8220;File not found!&#8221;<br \/>\nfi<\/p>\n<p>#!\/bin\/bash<br \/>\nFILE=$1<\/p>\n<p>if [ ! -f &#8220;$FILE&#8221; ]<br \/>\nthen<br \/>\n    echo &#8220;File $FILE does not exists&#8221;<br \/>\nfi<\/p>\n<p>if [[ ! -a $FILE ]]; then<br \/>\n    echo &#8220;$FILE does not exist!&#8221;<br \/>\nfi<\/p>\n<p>Also, it&#8217;s possible that the file is a broken symbolic link, or a non-regular file, <\/p>\n<p>like e.g. a socket, device or fifo. If you want to catch that you should:<\/p>\n<p>if [[ ! -a $FILE ]]; then<br \/>\n    if [[ -L $FILE ]]; then<br \/>\n        echo &#8220;$FILE is a broken symlink!&#8221;<br \/>\n    else<br \/>\n        echo &#8220;$FILE does not exist!&#8221;<br \/>\n    fi<br \/>\nfi<\/p>\n<p>To reverse a test, use &#8220;!&#8221;. That is equivalent to the &#8220;not&#8221; logical operator in other languages. Try this:<\/p>\n<p>if [ ! -f \/tmp\/test.txt ];<br \/>\nthen<br \/>\n    echo &#8220;File not found!&#8221;<br \/>\nfi<br \/>\nOr written in a slightly different way:<\/p>\n<p>if [ ! -f \/tmp\/test.txt ]<br \/>\n    then echo &#8220;File not found!&#8221;<br \/>\nfi<br \/>\nOr you could use:<\/p>\n<p>if ! [ -f \/tmp\/test.txt ]<br \/>\n    then echo &#8220;File not found!&#8221;<br \/>\nfi<br \/>\nOr, presing all together:<\/p>\n<p>if ! [ -f \/tmp\/test.txt ]; then echo &#8220;File not found!&#8221;; fi<br \/>\nWhich may be written (using then &#8220;and&#8221; operator: &#038;&#038;) as:<\/p>\n<p>[ ! -f \/tmp\/test.txt ] &#038;&#038; echo &#8220;File not found!&#8221;<br \/>\nWhich looks shorter like this:<\/p>\n<p>[ -f \/tmp\/test.txt ] || echo &#8220;File not found!&#8221;<\/p>\n<p>echo &#8220;entre file&#8221;<\/p>\n<p>read a<\/p>\n<p>if [ -s \/home\/rmohan\/$a ] <\/p>\n<p>then <\/p>\n<p>echo &#8220;yes file is there &#8221; <\/p>\n<p>else <\/p>\n<p>echo &#8220;soryy file is not there&#8221; <\/p>\n<p>fi<\/p>\n<p>if [ true ]<br \/>\nthen<br \/>\n  # do something here<br \/>\nfi<br \/>\nVery important: Make sure you leave spaces around the bracket characters.<\/p>\n<p>I&#8217;ll show more detailed tests as we go along.<\/p>\n<p>Linux shell file-related tests<br \/>\nTo perform tests on files use the following comparison operators:<\/p>\n<p>-d file    Test if file is a directory<br \/>\n-e file    Test if file exists<br \/>\n-f file    Test if file is an ordinary file<br \/>\n-r file    Test if file is readable<br \/>\n-w file    Test if file is writable<br \/>\n-x file    Test if file is executable<\/p>\n","protected":false},"excerpt":{"rendered":"<p>-e: Returns true value, if file exists<\/p>\n<p>-f: Return true value, if file exists and regular file<\/p>\n<p>-r: Return true value, if file exists and is readable<\/p>\n<p>-w: Return true value, if file exists and is writable<\/p>\n<p>-x: Return true value, if file exists and is executable<\/p>\n<p>-d: Return true value, if exists and is [&#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\/3337"}],"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=3337"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3337\/revisions"}],"predecessor-version":[{"id":3338,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3337\/revisions\/3338"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}