User Tools

Site Tools


bash:directories:check_if_a_directory_exists

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
bash:directories:check_if_a_directory_exists [2021/01/11 11:06] – created peterbash:directories:check_if_a_directory_exists [2021/01/26 12:06] (current) peter
Line 8: Line 8:
 fi fi
 </code> </code>
 +
 +<WRAP important>
 +**WARNING:**  A symbolic link to a directory will also not work, for example:
 +
 +<code bash>
 +ln -s "$ACTUAL_DIR" "$SYMLINK"
 +if [ -d "$SYMLINK" ]; then 
 +  rmdir "$SYMLINK" 
 +fi
 +</code>
 +
 +Will produce the error message:
 +
 +<code bash>
 +rmdir: failed to remove `symlink': Not a directory
 +</code>
 +
 +
 +So symbolic links may have to be treated differently, if subsequent commands expect directories:
 +
 +<code bash>
 +if [ -d "$LINK_OR_DIR" ]; then 
 +  if [ -L "$LINK_OR_DIR" ]; then
 +    # It is a symlink!
 +    # Symbolic link specific commands go here.
 +    rm "$LINK_OR_DIR"
 +  else
 +    # It's a directory!
 +    # Directory command goes here.
 +    rmdir "$LINK_OR_DIR"
 +  fi
 +fi
 +</code>
 +
 +</WRAP>
  
 ---- ----
bash/directories/check_if_a_directory_exists.1610363160.txt.gz · Last modified: 2021/01/11 11:06 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki