Checking if a directory exists using bash
The script to solve this issue is really easy to do and understand.
The first example checks if a directory exists and the second one if the directory does not exists:
if [ -d "$DIR" ]; then
echo "Directory exists"
fi
Or to check if a directory doesn't exist:
if [ ! -d "$DIR" ]; then
echo "Directory does not exists"
fi
For the negative we just use !
at the beginning.
If you are using symbolic links, the previous script will not work the proper way in some situations. The following bash script check if the path is a sym link:
if [ -d "$LINK_OR_DIR" ]; then
if [ -L "$LINK_OR_DIR" ]; then
echo "Symbolic link found"
else
echo "Directory found."
fi
fi