<p>What command checks if a directory exists or not within a Bash shell script?</p>

[{'answer_id': 59839, 'author': 'Grundlefleck', 'author_id': 4120, 'author_profile': 'https://Stackoverflow.com/users/4120', 'pm_score': 13, 'selected': True, 'text': '<p>To check if a directory exists:</p>\n<pre><code>if [ -d &quot;$DIRECTORY&quot; ]; then\n  echo &quot;$DIRECTORY does exist.&quot;\nfi\n</code></pre>\n<p>To check if a directory does not exist:</p>\n<pre><code>if [ ! -d &quot;$DIRECTORY&quot; ]; then\n  echo &quot;$DIRECTORY does not exist.&quot;\nfi\n</code></pre>\n<hr />\n<p>However, as <a href="https://stackoverflow.com/users/1438/jon-ericson">Jon Ericson</a> points out, subsequent commands may not work as intended if you do not take into account that a symbolic link to a directory will also pass this check.\nE.g. running this:</p>\n<pre><code>ln -s &quot;$ACTUAL_DIR&quot; &quot;$SYMLINK&quot;\nif [ -d &quot;$SYMLINK&quot; ]; then \n  rmdir &quot;$SYMLINK&quot; \nfi\n</code></pre>\n<p>Will produce the error message:</p>\n<pre class="lang-none prettyprint-override"><code>rmdir: failed to remove `symlink\': Not a directory\n</code></pre>\n<p>So symbolic links may have to be treated differently, if subsequent commands expect directories:</p>\n<pre><code>if [ -d &quot;$LINK_OR_DIR&quot; ]; then \n  if [ -L &quot;$LINK_OR_DIR&quot; ]; then\n    # It is a symlink!\n    # Symbolic link specific commands go here.\n    rm &quot;$LINK_OR_DIR&quot;\n  else\n    # It\'s a directory!\n    # Directory command goes here.\n    rmdir &quot;$LINK_OR_DIR&quot;\n  fi\nfi\n</code></pre>\n<hr />\n<p>Take particular note of the double-quotes used to wrap the variables. The reason for this is explained by 8jean <a href="https://stackoverflow.com/a/67458/102401">in another answer</a>.</p>\n<p>If the variables contain spaces or other unusual characters it will probably cause the script to fail.</p>\n'}, {'answer_id': 59853, 'author': 'Neil Neyman', 'author_id': 3240, 'author_profile': 'https://Stackoverflow.com/users/3240', 'pm_score': 5, 'selected': False, 'text': "<h2>More features using <code>find</code></h2>\n<ul>\n<li><p>Check existence of the folder within sub-directories:</p>\n<pre><code>  found=`find -type d -name &quot;myDirectory&quot;`\n  if [ -n &quot;$found&quot; ]\n  then\n      # The variable 'found' contains the full path where &quot;myDirectory&quot; is.\n      # It may contain several lines if there are several folders named &quot;myDirectory&quot;.\n  fi\n</code></pre>\n</li>\n<li><p>Check existence of one or several folders based on a pattern within the current directory:</p>\n<pre><code>  found=`find -maxdepth 1 -type d -name &quot;my*&quot;`\n  if [ -n &quot;$found&quot; ]\n  then\n      # The variable 'found' contains the full path where folders &quot;my*&quot; have been found.\n  fi\n</code></pre>\n</li>\n<li><p>Both combinations. In the following example, it checks the existence of the folder in the current directory:</p>\n<pre><code>  found=`find -maxdepth 1 -type d -name &quot;myDirectory&quot;`\n  if [ -n &quot;$found&quot; ]\n  then\n      # The variable 'found' is not empty =&gt; &quot;myDirectory&quot;` exists.\n  fi\n</code></pre>\n</li>\n</ul>\n"}, {'answer_id': 59871, 'author': 'Jon Ericson', 'author_id': 1438, 'author_profile': 'https://Stackoverflow.com/users/1438', 'pm_score': 8, 'selected': False, 'text': '<p>Note the <strong>-d</strong> test can produce some surprising results:</p>\n\n<pre><code>$ ln -s tmp/ t\n$ if [ -d t ]; then rmdir t; fi\nrmdir: directory "t": Path component not a directory\n</code></pre>\n\n<p>File under: "When is a directory not a directory?"  The answer: "When it\'s a symlink to a directory."  A slightly more thorough test:</p>\n\n<pre><code>if [ -d t ]; then \n   if [ -L t ]; then \n      rm t\n   else \n      rmdir t\n   fi\nfi\n</code></pre>\n\n<p>You can find more information in the Bash manual on <a href="https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions" rel="noreferrer">Bash conditional expressions</a> and the <a href="https://www.gnu.org/software/bash/manual/bash.html#index-_005b" rel="noreferrer"><code>[</code> builtin command</a> and the <a href="https://www.gnu.org/software/bash/manual/bash.html#index-_005b_005b" rel="noreferrer"><code>[[</code> compound commmand</a>.</p>\n'}, {'answer_id': 59969, 'author': 'elmarco', 'author_id': 1277510, 'author_profile': 'https://Stackoverflow.com/users/1277510', 'pm_score': 8, 'selected': False, 'text': '<p>Shorter form:</p>\n<pre class="lang-sh prettyprint-override"><code># if $DIR is a directory, then print yes\n[ -d &quot;$DIR&quot; ] &amp;&amp; echo &quot;Yes&quot;\n</code></pre>\n'}, {'answer_id': 60014, 'author': 'yukondude', 'author_id': 726, 'author_profile': 'https://Stackoverflow.com/users/726', 'pm_score': 8, 'selected': False, 'text': '<p>I find the <a href="http://tldp.org/LDP/abs/html/testconstructs.html#DBLBRACKETS" rel="noreferrer">double-bracket</a> version of <code>test</code> makes writing logic tests more natural:</p>\n\n<pre><code>if [[ -d "${DIRECTORY}" &amp;&amp; ! -L "${DIRECTORY}" ]] ; then\n    echo "It\'s a bona-fide directory"\nfi\n</code></pre>\n'}, {'answer_id': 67458, 'author': '8jean', 'author_id': 10011, 'author_profile': 'https://Stackoverflow.com/users/10011', 'pm_score': 9, 'selected': False, 'text': "<p>Always wrap variables in double quotes when referencing them in a Bash script.</p>\n<pre><code>if [ -d &quot;$DIRECTORY&quot; ]; then\n    # Will enter here if $DIRECTORY exists, even if it contains spaces\nfi\n</code></pre>\n<p>Kids these days put spaces and lots of other funny characters in their directory names. (Spaces! Back in my day, we didn't have no fancy spaces!)\nOne day, one of those kids will run your script with <code>$DIRECTORY</code> set to <code>&quot;My M0viez&quot;</code> and your script will blow up. You don't want that. So use double quotes.</p>\n"}, {'answer_id': 1288121, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 6, 'selected': False, 'text': '<p>Or for something completely useless:</p>\n\n<pre><code>[ -d . ] || echo "No"\n</code></pre>\n'}, {'answer_id': 2283055, 'author': 'e-satis', 'author_id': 9951, 'author_profile': 'https://Stackoverflow.com/users/9951', 'pm_score': 5, 'selected': False, 'text': '<p>Actually, you should use several tools to get a bulletproof approach:</p>\n\n<pre><code>DIR_PATH=`readlink -f "${the_stuff_you_test}"` # Get rid of symlinks and get abs path\nif [[ -d "${DIR_PATH}" ]] ; Then # Now you\'re testing\n    echo "It\'s a dir";\nfi\n</code></pre>\n\n<p>There isn\'t any need to worry about spaces and special characters as long as you use <code>"${}"</code>.</p>\n\n<p>Note that <code>[[]]</code> is not as portable as <code>[]</code>, but since most people work with modern versions of Bash (since after all, most people don\'t even work with command line :-p), the benefit is greater than the trouble.</p>\n'}, {'answer_id': 2469169, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<pre><code>if [ -d "$DIRECTORY" ]; then\n    # Will enter here if $DIRECTORY exists\nfi\n</code></pre>\n\n<p>This is not completely true...</p>\n\n<p>If you want to go to that directory, you also need to have the execute rights on the directory. Maybe you need to have write rights as well.</p>\n\n<p>Therefore:</p>\n\n<pre><code>if [ -d "$DIRECTORY" ] &amp;&amp; [ -x "$DIRECTORY" ] ; then\n    # ... to go to that directory (even if DIRECTORY is a link)\n    cd $DIRECTORY\n    pwd\nfi\n</code></pre>\n\n<hr>\n\n<pre><code>if [ -d "$DIRECTORY" ] &amp;&amp; [ -w "$DIRECTORY" ] ; then\n    # ... to go to that directory and write something there (even if DIRECTORY is a link)\n    cd $DIRECTORY\n    touch foobar\nfi\n</code></pre>\n'}, {'answer_id': 2680681, 'author': 'muralikrishna', 'author_id': 321968, 'author_profile': 'https://Stackoverflow.com/users/321968', 'pm_score': 5, 'selected': False, 'text': '<pre><code>if [ -d "$Directory" -a -w "$Directory" ]\nthen\n    #Statements\nfi\n</code></pre>\n\n<p>The above code checks if the directory exists and if it is writable.</p>\n'}, {'answer_id': 7436646, 'author': 'ztank1013', 'author_id': 938615, 'author_profile': 'https://Stackoverflow.com/users/938615', 'pm_score': 3, 'selected': False, 'text': '<p>The <code>ls</code> command in conjunction with <code>-l</code> (long listing) option returns attributes information about files and directories.<br>\nIn particular the first character of <code>ls -l</code> output it is usually a <code>d</code> or a <code>-</code> (dash). In case of a <code>d</code> the one listed is a directory for sure.</p>\n\n<p>The following command in just one line will tell you if the given <strong><code>ISDIR</code></strong> variable contains a path to a directory or not:</p>\n\n<pre><code>[[ $(ls -ld "$ISDIR" | cut -c1) == \'d\' ]] &amp;&amp;\n    echo "YES, $ISDIR is a directory." || \n    echo "Sorry, $ISDIR is not a directory"\n</code></pre>\n\n<p>Practical usage:</p>\n\n<pre><code>    [claudio@nowhere ~]$ ISDIR="$HOME/Music" \n    [claudio@nowhere ~]$ ls -ld "$ISDIR"\n    drwxr-xr-x. 2 claudio claudio 4096 Aug 23 00:02 /home/claudio/Music\n    [claudio@nowhere ~]$ [[ $(ls -ld "$ISDIR" | cut -c1) == \'d\' ]] &amp;&amp; \n        echo "YES, $ISDIR is a directory." ||\n        echo "Sorry, $ISDIR is not a directory"\n    YES, /home/claudio/Music is a directory.\n\n    [claudio@nowhere ~]$ touch "empty file.txt"\n    [claudio@nowhere ~]$ ISDIR="$HOME/empty file.txt" \n    [claudio@nowhere ~]$ [[ $(ls -ld "$ISDIR" | cut -c1) == \'d\' ]] &amp;&amp; \n        echo "YES, $ISDIR is a directory." || \n        echo "Sorry, $ISDIR is not a directoy"\n    Sorry, /home/claudio/empty file.txt is not a directory\n</code></pre>\n'}, {'answer_id': 8480518, 'author': 'dromichaetes', 'author_id': 1094365, 'author_profile': 'https://Stackoverflow.com/users/1094365', 'pm_score': 3, 'selected': False, 'text': '<p>There are great solutions out there, but ultimately every script will fail if you\'re not in the right directory. So code like this:</p>\n\n<pre><code>if [ -d "$LINK_OR_DIR" ]; then\nif [ -L "$LINK_OR_DIR" ]; then\n    # It is a symlink!\n    # Symbolic link specific commands go here\n    rm "$LINK_OR_DIR"\nelse\n    # It\'s a directory!\n    # Directory command goes here\n    rmdir "$LINK_OR_DIR"\nfi\nfi\n</code></pre>\n\n<p>will execute successfully only if at the moment of execution you\'re in a directory that has a subdirectory that you happen to check for.</p>\n\n<p>I understand the initial question like this: to verify if a directory exists irrespective of the user\'s position in the file system. So using the command \'find\' might do the trick:</p>\n\n<pre><code>dir=" "\necho "Input directory name to search for:"\nread dir\nfind $HOME -name $dir -type d\n</code></pre>\n\n<p>This solution is good because it allows the use of wildcards, a useful feature when searching for files/directories. The only problem is that, if the searched directory doesn\'t exist, the \'find\' command will print nothing to <a href="https://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29" rel="nofollow noreferrer">standard output</a> (not an elegant solution for my taste) and will have nonetheless a zero exit. Maybe someone could improve on this.</p>\n'}, {'answer_id': 11892411, 'author': 'Henk Langeveld', 'author_id': 667820, 'author_profile': 'https://Stackoverflow.com/users/667820', 'pm_score': 6, 'selected': False, 'text': '<p>Here\'s a very pragmatic idiom:</p>\n\n<pre><code>(cd $dir) || return # Is this a directory,\n                    # and do we have access?\n</code></pre>\n\n<p>I typically wrap it in a function:</p>\n\n<pre><code>can_use_as_dir() {\n    (cd ${1:?pathname expected}) || return\n}\n</code></pre>\n\n<p>Or:</p>\n\n<pre><code>assert_dir_access() {\n    (cd ${1:?pathname expected}) || exit\n}\n</code></pre>\n\n<p>The nice thing about this approach is that I do not have to think of a good error message.</p>\n\n<p><code>cd</code> will give me a standard one line message to <a href="https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)" rel="noreferrer">standard error</a> already. It will also give more information than I will be able to provide. By performing the <code>cd</code> inside a subshell <code>( ... )</code>, the command does not affect the current directory of the caller.  If the directory exists, this subshell and the function are just a no-op.</p>\n\n<p>Next is the argument that we pass to <code>cd</code>: <code>${1:?pathname expected}</code>.  This is a more elaborate form of parameter substitution which is explained in more detail below.</p>\n\n<p>Tl;dr: If the string passed into this function is empty, we again exit from the subshell <code>( ... )</code> and return from the function with the given error message.</p>\n\n<hr>\n\n<p>Quoting from the <code>ksh93</code> man page:</p>\n\n<pre><code>${parameter:?word}\n</code></pre>\n\n<blockquote>\n  <p>If  <code>parameter</code>  is set and is non-null then substitute its value;\n  otherwise, print <code>word</code> and exit from the shell (if not interactive).\n  If <code>word</code> is omitted then a standard message is printed.</p>\n</blockquote>\n\n<p>and</p>\n\n<blockquote>\n  <p>If the colon <code>:</code> is omitted from  the  above  expressions,  then  the\n  shell only checks whether parameter is set or not.</p>\n</blockquote>\n\n<p>The phrasing here is peculiar to the shell documentation, as <code>word</code> may refer to any reasonable string, including whitespace.</p>\n\n<p>In this particular case, I know that the standard error message <code>1: parameter not set</code> is not sufficient, so I zoom in on the type of value that we expect here - the <code>pathname</code> of a directory.</p>\n\n<p>A philosophical note:</p>\n\n<p>The shell is not an object oriented language, so the message says <code>pathname</code>, not <code>directory</code>. At this level, I\'d rather keep it simple - the arguments to a function are just strings.</p>\n'}, {'answer_id': 15015952, 'author': 'ajmartin', 'author_id': 477522, 'author_profile': 'https://Stackoverflow.com/users/477522', 'pm_score': 3, 'selected': False, 'text': '<pre><code>file="foo" \nif [[ -e "$file" ]]; then echo "File Exists"; fi;\n</code></pre>\n'}, {'answer_id': 16093972, 'author': 'Juan Carlos Kuri Pinto', 'author_id': 1408995, 'author_profile': 'https://Stackoverflow.com/users/1408995', 'pm_score': 4, 'selected': False, 'text': '<pre><code>[ -d ~/Desktop/TEMPORAL/ ] &amp;&amp; echo "DIRECTORY EXISTS" || echo "DIRECTORY DOES NOT EXIST"\n</code></pre>\n'}, {'answer_id': 17859049, 'author': 'bailey86', 'author_id': 450406, 'author_profile': 'https://Stackoverflow.com/users/450406', 'pm_score': 4, 'selected': False, 'text': '<p>Using the <code>-e</code> check will check for files and this includes directories.</p>\n\n<pre><code>if [ -e ${FILE_PATH_AND_NAME} ]\nthen\n    echo "The file or directory exists."\nfi\n</code></pre>\n'}, {'answer_id': 20978574, 'author': 'derFunk', 'author_id': 591004, 'author_profile': 'https://Stackoverflow.com/users/591004', 'pm_score': 3, 'selected': False, 'text': '<p>If you want to check if a directory exists, regardless if it\'s a real directory or a symlink, use this:</p>\n\n<pre><code>ls $DIR\nif [ $? != 0 ]; then\n        echo "Directory $DIR already exists!"\n        exit 1;\nfi\necho "Directory $DIR does not exist..."\n</code></pre>\n\n<p>Explanation: The "ls" command gives an error "ls: /x: No such file or directory" if the directory or symlink does not exist, and also sets the return code, which you can retrieve via "$?", to non-null (normally "1").\nBe sure that you check the return code directly after calling "ls".</p>\n'}, {'answer_id': 25287796, 'author': 'Sadhun', 'author_id': 3455684, 'author_profile': 'https://Stackoverflow.com/users/3455684', 'pm_score': 3, 'selected': False, 'text': '<p>The below <code>find</code> can be used,</p>\n\n<pre><code>find . -type d -name dirname -prune -print\n</code></pre>\n'}, {'answer_id': 29505062, 'author': 'Jorge Barroso', 'author_id': 4761359, 'author_profile': 'https://Stackoverflow.com/users/4761359', 'pm_score': 7, 'selected': False, 'text': '<p>To check if a directory exists you can use a simple <code>if</code> structure like this:</p>\n\n<pre><code>if [ -d directory/path to a directory ] ; then\n# Things to do\n\nelse #if needed #also: elif [new condition]\n# Things to do\nfi\n</code></pre>\n\n<p>You can also do it in the negative:</p>\n\n<pre><code>if [ ! -d directory/path to a directory ] ; then\n# Things to do when not an existing directory\n</code></pre>\n\n<p><strong>Note</strong>: Be careful. Leave empty spaces on either side of both opening and closing braces.</p>\n\n<p>With the same syntax you can use:</p>\n\n<pre><code>-e: any kind of archive\n\n-f: file\n\n-h: symbolic link\n\n-r: readable file\n\n-w: writable file\n\n-x: executable file\n\n-s: file size greater than zero\n</code></pre>\n'}, {'answer_id': 29724604, 'author': 'Jahid', 'author_id': 3744681, 'author_profile': 'https://Stackoverflow.com/users/3744681', 'pm_score': 4, 'selected': False, 'text': '<pre><code>[[ -d "$DIR" &amp;&amp; ! -L "$DIR" ]] &amp;&amp; echo "It\'s a directory and not a symbolic link"\n</code></pre>\n\n<blockquote>\n  <p>N.B: Quoting variables is a good practice.</p>\n</blockquote>\n\n<p><strong>Explanation:</strong></p>\n\n<ul>\n<li><code>-d</code>: check if it\'s a directory</li>\n<li><code>-L</code>: check if it\'s a symbolic link</li>\n</ul>\n'}, {'answer_id': 30208219, 'author': 'Piyush Baijal', 'author_id': 2458462, 'author_profile': 'https://Stackoverflow.com/users/2458462', 'pm_score': 3, 'selected': False, 'text': '<p>(1)</p>\n\n<pre><code>[ -d Piyush_Drv1 ] &amp;&amp; echo ""Exists"" || echo "Not Exists"\n</code></pre>\n\n<p>(2)</p>\n\n<pre><code>[ `find . -type d -name Piyush_Drv1 -print | wc -l` -eq 1 ] &amp;&amp; echo Exists || echo "Not Exists"\n</code></pre>\n\n<p>(3)</p>\n\n<pre><code>[[ -d run_dir  &amp;&amp; ! -L run_dir ]] &amp;&amp; echo Exists || echo "Not Exists"\n</code></pre>\n\n<p>If an issue is found with one of the approaches provided above:</p>\n\n<p>With the <code>ls</code> command; the cases when a directory does not exists - an error message is shown</p>\n\n<pre><code>[[ `ls -ld SAMPLE_DIR| grep ^d | wc -l` -eq 1 ]] &amp;&amp; echo exists || not exists\n</code></pre>\n\n<blockquote>\n  <p>-ksh: not: not found [No such file or directory]</p>\n</blockquote>\n'}, {'answer_id': 32543846, 'author': 'kenorb', 'author_id': 55075, 'author_profile': 'https://Stackoverflow.com/users/55075', 'pm_score': 3, 'selected': False, 'text': '<p>As per <a href="https://stackoverflow.com/users/15168/jonathan-leffler">Jonathan</a>\'s comment:</p>\n\n<p>If you want to create the directory and it does not exist yet, then the simplest technique is to use <code>mkdir -p</code> which creates the directory — and any missing directories up the path — and does not fail if the directory already exists, so you can do it all at once with:</p>\n\n<pre><code>mkdir -p /some/directory/you/want/to/exist || exit 1\n</code></pre>\n'}, {'answer_id': 32543895, 'author': 'kenorb', 'author_id': 55075, 'author_profile': 'https://Stackoverflow.com/users/55075', 'pm_score': 7, 'selected': False, 'text': '<p>You can use <code>test -d</code> (see <code>man test</code>).</p>\n\n<blockquote>\n  <p><code>-d file</code>       True if file exists and is a directory.</p>\n</blockquote>\n\n<p>For example:</p>\n\n<pre><code>test -d "/etc" &amp;&amp; echo Exists || echo Does not exist\n</code></pre>\n\n<p>Note: The <code>test</code> command is same as conditional expression <code>[</code> (see: <code>man [</code>), so it\'s portable across shell scripts.</p>\n\n<blockquote>\n  <p><code>[</code> - This is a synonym for the <code>test</code> builtin, but the last argument must, be a literal <code>]</code>, to match the opening <code>[</code>.</p>\n</blockquote>\n\n<p>For possible options or further help, check:</p>\n\n<ul>\n<li><code>help [</code></li>\n<li><code>help test</code></li>\n<li><code>man test</code> or <code>man [</code></li>\n</ul>\n'}, {'answer_id': 34508422, 'author': 'Raamesh Keerthi', 'author_id': 1116081, 'author_profile': 'https://Stackoverflow.com/users/1116081', 'pm_score': 4, 'selected': False, 'text': '<p>To check more than one directory use this code:</p>\n\n<pre><code>if [ -d "$DIRECTORY1" ] &amp;&amp; [ -d "$DIRECTORY2" ] then\n    # Things to do\nfi\n</code></pre>\n'}, {'answer_id': 36172057, 'author': 'David Okwii', 'author_id': 547050, 'author_profile': 'https://Stackoverflow.com/users/547050', 'pm_score': 4, 'selected': False, 'text': '<p>Check if the directory exists, else make one:</p>\n\n<pre><code>[ -d "$DIRECTORY" ] || mkdir $DIRECTORY\n</code></pre>\n'}, {'answer_id': 36654179, 'author': 'Brad Parks', 'author_id': 26510, 'author_profile': 'https://Stackoverflow.com/users/26510', 'pm_score': 4, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/a/59871/26510">This answer</a> wrapped up as a shell script</p>\n\n<h3>Examples</h3>\n\n<pre><code>$ is_dir ~                           \nYES\n\n$ is_dir /tmp                        \nYES\n\n$ is_dir ~/bin                       \nYES\n\n$ mkdir \'/tmp/test me\'\n\n$ is_dir \'/tmp/test me\'\nYES\n\n$ is_dir /asdf/asdf                  \nNO\n\n# Example of calling it in another script\nDIR=~/mydata\nif [ $(is_dir $DIR) == "NO" ]\nthen\n  echo "Folder doesnt exist: $DIR";\n  exit;\nfi\n</code></pre>\n\n<h3>is_dir</h3>\n\n<pre><code>function show_help()\n{\n  IT=$(CAT &lt;&lt;EOF\n\n  usage: DIR\n  output: YES or NO, depending on whether or not the directory exists.\n\n  )\n  echo "$IT"\n  exit\n}\n\nif [ "$1" == "help" ]\nthen\n  show_help\nfi\nif [ -z "$1" ]\nthen\n  show_help\nfi\n\nDIR=$1\nif [ -d $DIR ]; then \n   echo "YES";\n   exit;\nfi\necho "NO";\n</code></pre>\n'}, {'answer_id': 43426718, 'author': 'Abhishek Gurjar', 'author_id': 5345150, 'author_profile': 'https://Stackoverflow.com/users/5345150', 'pm_score': 3, 'selected': False, 'text': '<p>In kind of a <a href="https://en.wikipedia.org/wiki/%3F:" rel="noreferrer">ternary</a> form,</p>\n\n<pre><code>[ -d "$directory" ] &amp;&amp; echo "exist" || echo "not exist"\n</code></pre>\n\n<p>And with <a href="https://linux.die.net/man/1/test" rel="noreferrer"><code>test</code></a>:</p>\n\n<pre><code>test -d "$directory" &amp;&amp; echo "exist" || echo "not exist"\n</code></pre>\n'}, {'answer_id': 44835790, 'author': 'Gene', 'author_id': 2057089, 'author_profile': 'https://Stackoverflow.com/users/2057089', 'pm_score': 2, 'selected': False, 'text': '<p>From script file <em>myScript.sh</em>:</p>\n\n<pre><code>if [ -d /home/ec2-user/apache-tomcat-8.5.5/webapps/Gene\\ Directory ]; then\n   echo "Directory exists!"\n   echo "Great"\nfi\n</code></pre>\n\n<p><em>Or</em></p>\n\n<pre><code>if [ -d \'/home/ec2-user/apache-tomcat-8.5.5/webapps/Gene Directory\' ]; then\n   echo "Directory exists!"\n   echo "Great"\nfi\n</code></pre>\n'}, {'answer_id': 46205746, 'author': 'KANJICODER', 'author_id': 1740154, 'author_profile': 'https://Stackoverflow.com/users/1740154', 'pm_score': 2, 'selected': False, 'text': '<p><strong>Git Bash + Dropbox + Windows:</strong></p>\n\n<p>None of the other solutions worked for my Dropbox folder,\nwhich was weird because I can Git push to a Dropbox symbolic path.</p>\n\n<pre><code>#!/bin/bash\n\ndbox="~/Dropbox/"\nresult=0\nprv=$(pwd) &amp;&amp; eval "cd $dbox" &amp;&amp; result=1 &amp;&amp; cd "$prv"\necho $result\n\nread -p "Press Enter To Continue:"\n</code></pre>\n\n<p>You\'ll probably want to know how to successfully navigate to Dropbox from Bash as well. So here is the script in its entirety.</p>\n\n<p><a href="https://pastebin.com/QF2Exmpn" rel="nofollow noreferrer">https://pastebin.com/QF2Exmpn</a></p>\n'}, {'answer_id': 47699462, 'author': 'ArtOfWarfare', 'author_id': 901641, 'author_profile': 'https://Stackoverflow.com/users/901641', 'pm_score': 5, 'selected': False, 'text': "<p>Have you considered just doing whatever you want to do in the <code>if</code> rather than looking before you leap?</p>\n\n<p>I.e., if you want to check for the existence of a directory before you enter it, try just doing this:</p>\n\n<pre><code>if pushd /path/you/want/to/enter; then\n    # Commands you want to run in this directory\n    popd\nfi\n</code></pre>\n\n<p>If the path you give to <code>pushd</code> exists, you'll enter it and it'll exit with <code>0</code>, which means the <code>then</code> portion of the statement will execute. If it doesn't exist, nothing will happen (other than some output saying the directory doesn't exist, which is probably a helpful side-effect anyways for debugging).</p>\n\n<p>It seems better than this, which requires repeating yourself:</p>\n\n<pre><code>if [ -d /path/you/want/to/enter ]; then\n    pushd /path/you/want/to/enter\n    # Commands you want to run in this directory\n    popd\nfi\n</code></pre>\n\n<p>The same thing works with <code>cd</code>, <code>mv</code>, <code>rm</code>, etc... if you try them on files that don't exist, they'll exit with an error and print a message saying it doesn't exist, and your <code>then</code> block will be skipped. If you try them on files that do exist, the command will execute and exit with a status of <code>0</code>, allowing your <code>then</code> block to execute.</p>\n"}, {'answer_id': 50342967, 'author': 'yoctotutor.com', 'author_id': 6484851, 'author_profile': 'https://Stackoverflow.com/users/6484851', 'pm_score': 7, 'selected': False, 'text': '<ol>\n<li><p>A simple script to test if a directory or file is present or not:</p>\n<pre><code> if [ -d /home/ram/dir ]   # For file &quot;if [ -f /home/rama/file ]&quot;\n then\n     echo &quot;dir present&quot;\n else\n     echo &quot;dir not present&quot;\n fi\n</code></pre>\n</li>\n<li><p>A simple script to check whether the directory is present or not:</p>\n<pre><code> mkdir tempdir   # If you want to check file use touch instead of mkdir\n ret=$?\n if [ &quot;$ret&quot; == &quot;0&quot; ]\n then\n     echo &quot;dir present&quot;\n else\n     echo &quot;dir not present&quot;\n fi\n</code></pre>\n<p>The above scripts will check if the directory is present or not</p>\n<p><code>$?</code>  if the last command is a success it returns &quot;0&quot;, else a non-zero value.\nSuppose <code>tempdir</code> is already present. Then <code>mkdir tempdir</code> will give an error like below:</p>\n<blockquote>\n<p>mkdir: cannot create directory ‘tempdir’: File exists</p>\n</blockquote>\n</li>\n</ol>\n'}, {'answer_id': 51017848, 'author': 'Sudip Bhandari', 'author_id': 4589003, 'author_profile': 'https://Stackoverflow.com/users/4589003', 'pm_score': 3, 'selected': False, 'text': "<p>Use the <code>file</code> program.\nConsidering all directories are also files in Linux, issuing the following command would suffice:</p>\n\n<p><code>file $directory_name</code></p>\n\n<p>Checking a nonexistent file: <code>file blah</code></p>\n\n<p><strong>Output:</strong> <code>cannot open 'blah' (No such file or directory)</code></p>\n\n<p>Checking an existing directory: <code>file bluh</code></p>\n\n<p><strong>Output:</strong> <code>bluh: directory</code></p>\n"}, {'answer_id': 57223290, 'author': 'Bayou', 'author_id': 8382929, 'author_profile': 'https://Stackoverflow.com/users/8382929', 'pm_score': 2, 'selected': False, 'text': '<p>Just as an alternative to the \'[ -d ]\' and \'[ -h ]\' options, you can make use of <code>stat</code> to obtain the file type and parse it.</p>\n\n<pre><code>#! /bin/bash\nMY_DIR=$1\nNODE_TYPE=$(stat -c \'%F\' ${MY_DIR} 2&gt;/dev/null)\ncase "${NODE_TYPE}" in\n        "directory") echo $MY_DIR;;\n    "symbolic link") echo $(readlink $MY_DIR);;\n                 "") echo "$MY_DIR does not exist";;\n                  *) echo "$NODE_TYPE is unsupported";;\nesac\nexit 0\n</code></pre>\n\n<p>Test data:</p>\n\n<pre><code>$ mkdir tmp\n$ ln -s tmp derp\n$ touch a.txt\n$ ./dir.sh tmp\ntmp\n$ ./dir.sh derp\ntmp\n$ ./dir.sh a.txt\nregular file is unsupported\n$ ./dir.sh god\ngod does not exist\n</code></pre>\n'}, {'answer_id': 58297948, 'author': 'Vishal', 'author_id': 197473, 'author_profile': 'https://Stackoverflow.com/users/197473', 'pm_score': 5, 'selected': False, 'text': '<pre class="lang-sh prettyprint-override"><code>DIRECTORY=/tmp\n\nif [ -d "$DIRECTORY" ]; then\n    echo "Exists"\nfi\n</code></pre>\n\n<p><strong><a href="https://code.labstack.com/7GMFlEaC" rel="noreferrer">Try online</a></strong></p>\n'}, {'answer_id': 71447129, 'author': 'Aybak3k', 'author_id': 16146522, 'author_profile': 'https://Stackoverflow.com/users/16146522', 'pm_score': 3, 'selected': False, 'text': '<p>One Liner:</p>\n<pre class="lang-sh prettyprint-override"><code>[[ -d $Directory ]] &amp;&amp; echo true\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/59838', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4120/']