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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux system|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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:
- The first method is by using single brackets
[ ]and the
-doperator in your
ifstatement, 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
$DIRhappens to be a file, the script will still say that the directory does not exist.
- 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."
- 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
- 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
- What if we want to check whether or not multiple directories exist? In that case, it is useful to string together multiple
-aas 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
if [ -d "$DIR1" -a -d "$DIR2" ]; then echo "Both $DIR1 and $DIR2 directories exist!" fi
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
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.