Bash Scripting: Check if directory exists

When writing a Bash script, it is common that you’ll run into the need to check for the existence of a directory. Based on the result, your Bash script can proceed with the appropriate action.

This functionality can be written into a Bash script or used directly from the command line, without writing a script for it. In this tutorial, you will see how to check if a directory exists in Bash on Linux systems.

In this tutorial you will learn:

  • How to check if a directory exists in Bash script
  • How to check if a directory exists from Bash script
Bash Scripting: Check if directory exists
Bash Scripting: Check if directory exists
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux system
Software Bash shell
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Check if directory exists in Bash script




There are multiple ways to check if a directory exists, see the methods below:

  1. The first method is by using single brackets [ ] and the -d operator in your if statement, like in the below script:
    DIR=/tmp/downloads
    if [ -d "$DIR" ];
    then
        echo "$DIR directory exists."
    else
    	echo "$DIR directory does not exist."
    fi
    

    Note that if $DIR happens to be a file, the script will still say that the directory does not exist.

  2. The next method is a little more succinct, and easier to use on the command line.
    DIR=/tmp/downloads
    [ -d "$DIR" ] && echo "$DIR directory exists."
    

    A command line one-liner would look like this:

    $ DIR=/tmp/downloads; [ -d "$DIR" ] && echo "$DIR directory exists."
    OR
    $ [ -d /tmp/downloads ] && echo "the directory exists."
    
  3. Note that you can also use double brackers [[ ]] in either of the previous examples.
    DIR=/tmp/downloads
    if [[ -d "$DIR" ]];
    then
        echo "$DIR directory exists."
    else
    	echo "$DIR directory does not exist."
    fi
    
  4. We can also check to see if a directory does not exist, by using the ! operator – which is used to negate expressions in Bash.
    DIR=/tmp/downloads
    if [ ! -d "$DIR" ];
    then
    	echo "$DIR directory does not exist."
    else
    	echo "$DIR directory exists."
    fi
    
  5. What if we want to check whether or not multiple directories exist? In that case, it is useful to string together multiple if conditions with && operators or -a as seen below. Note that using the && operator will also require you to use double brackets [[ ]].


    if [[ -d "$DIR1" && -d "$DIR2" ]]; then
        echo "Both $DIR1 and $DIR2 directories exist!"
    fi
    

    OR

    if [ -d "$DIR1" -a -d "$DIR2" ]; then
        echo "Both $DIR1 and $DIR2 directories exist!"
    fi
    
NOTE
Be sure to put your directory in double quotes " " if it contains spaces. Alternatively, wrap the directory’s variable in quotes, such as "$DIR" in our examples. This will prevent you from encountering some error. However, you don’t need to wrap the variable in quotes in the echo lines.

Closing Thoughts

In this tutorial, we saw how to check if a directory exists from a Bash script or from the command line in Linux. This is a very useful function written into tons of Bash scripts, as many can only proceed if certain directories are already known to exist. This takes the guess work out of the equation and will tell your script exactly how to proceed, as checking for the existence of a directory only takes the Bash shell a fraction of a second.



Comments and Discussions
Linux Forum