Checking if a directory exists

Bash:
Checking if a directory exists

How to:

At its core, Bash allows you to check for the existence of a directory using conditional statements and the -d operator. Below is a straightforward example that demonstrates how to perform this check.

if [ -d "/path/to/directory" ]; then
    echo "The directory exists."
else
    echo "The directory does not exist."
fi

Sample Output (if the directory exists):

The directory exists.

Sample Output (if the directory does not exist):

The directory does not exist.

For more complex scripts, it’s common to combine the check with other operations, such as creating the directory if it doesn’t exist:

DIR="/path/to/directory"
if [ -d "$DIR" ]; then
    echo "$DIR exists."
else
    echo "$DIR does not exist. Creating now..."
    mkdir -p "$DIR"
    echo "$DIR created."
fi

Sample Output (if the directory does not exist and then is created):

/path/to/directory does not exist. Creating now...
/path/to/directory created.

Though Bash itself provides robust tools for such checks, there are no popular third-party libraries specifically for this task, as native Bash commands are fully capable and efficient for directory presence validation.