How Do I Check if a Directory Exists or Not in a Bash Shell Script?

In Linux, checking the existence of the directory is necessary before performing any operation on it. Fortunately, the bash script facilitates the user to check the existence of the particular directory by making a script. The user can examine the “d” operator to deal with the directory-related tasks in the bash script.

The subsequent post will describe the methods to check the existence of the directory in a Bash Shell Script. 

  • Using -d Operator
  • Using -d and && Operator
  • How to Deal with Directory Having Symbolic Link?

Method 1: Check the Existence of the Directory Using -d Operator

The following script will take the directory path/name from the user and will display the appropriate message about the existence of the directory:

#!/bin/bash

read -p "Enter Directory Path/Name: " DIR

if [ -d "$DIR" ];
then
    echo "The Given Directory $DIR Exists."
else
        echo "There is no Existence of the Directory $DIR"
fi

The script is defined as:

  • The “read” property to take the directory path/name from the users in “DIR.”
  • The “d” operator in the “if” to check the existence of the directory entered by the user.
  • The “then” section prints messages through echo if the directory exists.
  • The “else” portion displays the message through echo if the directory doesn’t exist.

Save the script file and run it using the bash command in the terminal:

$ bash script.sh

The given directory “Henry” exists as shown.

Method 2: Check the Existence of the Directory Using ! and -d

The user can also use the “!” operator to check the directory existence as shown in the following script:

#!/bin/bash

read -p "Enter Directory Path/Name: " DIR

if [ ! -d "$DIR" ];
then
    echo "There is no Existence of the Directory $DIR"
else
      echo "The Given Directory $DIR Exists."
fi

Save the above script and run it in the terminal:

$ bash script.sh

The given directory path “/home/dir” doesn’t exist.

Method 3: Check the Existence of the Directory Using -d and && Operator

Another possible way to check the existence of the directory is by using the “d” and “&&” operators as utilized in the below script:

#!/bin/bash

read -p "Enter Directory Name/Path: " DIR

[ -d "$DIR" ] && echo "$DIR Exist."

The script is defined as:

  • The “read” property to take the directory path/name from the users in “DIR.”
  • The “[ -d “$DIR” ]” expression to check the existence of the entered directory.
  • The “&&” operator and echo statement is only executed if the directory exists.

Save the above script in the file and run it in the terminal:

$ bash script.sh

The entered directory “Downloads” exist in the directory.

How to Deal With Directory Having Symbolic Link?

Sometimes the entered directory may be a symbolic link directory, and the bash script also deals with the symbolic link directory through the “L” operator. The user needs to use the nested if/else in the bash script for that purpose. The following script will check whether the entered directory is a symbolic link or a regular directory:

#!/bin/bash

read -p "Enter Directory Path/Name: " DIR

if [ -d "$DIR" ];
then
if [ -L "$DIR" ]; then
    echo "The $DIR is a Symbolic Link Directory."
  else
    echo "The $DIR is a Regular Directory."
  fi
else
        echo "There is no Existence of the Directory $DIR"
fi

The script is defined as: 

  • The “read” property to take the directory path/name from the users in “DIR.”
  • The “d” operator in the “if” to check the existence of the directory entered by the user.
  • The “then” section further checks if the directory is a symbolic link or a regular directory using the “L” operator.
  • The last “else” portion will be executed if the directory doesn’t exist. 

Save the script in a file and exit from the editor. 

Check the script by entering a symbolic link directory:

$ bash script.sh

The given directory “S-Henry” is a symbolic link directory.

Conclusion

In the bash script, to check whether the directory exists or not, utilize the “d” operator or “L” operator if the directory is symbolically linked. The user can also use the “!” and “&&” operator along with the “d” and “L” operators to check the existence of the directory.

This write-up has illuminated the examples to check the existence of the directory in the bash script.