HowTo: Check If a Directory Exists In a Shell Script

by on April 14, 2013 · 3 comments· LAST UPDATED August 23, 2013

in , ,

How do I check if a directory exists in a shell script under Linux or Unix like operating systems?

To check if a directory exists and is a directory use the following syntax:

[ -d "/path/to/dir" ] && echo "Directory /path/to/dir exists." || echo "Error: Directory /path/to/dir does not exists."

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsBash/Linux
Estimated completion time5m
The following version also check for symbolic link:

[ -d "/path/to/dir" && ! -L "/path/to/dir" ] && echo "Directory /path/to/dir exists." || echo "Error: Directory /path/to/dir exists but point to $(readlink -f /path/to/dir)."

OR

[ -d "/path/to/dir" && ! -h "/path/to/dir" ] && echo "Directory /path/to/dir exists." || echo "Error: Directory /path/to/dir exists but point to $(readlink -f /path/to/dir)."

Finally, you can use the traditional if..else..fi bash syntax as follows:

if [ -d "/path/to/dir" ]
then
    echo "Directory /path/to/dir exists."
else
    echo "Error: Directory /path/to/dir does not exists."
fi

Shell script examples to see if a ${directory} exists or not

The following script also demos the use of readlink command to print value of a symbolic link or canonical file name.

 
#!/bin/bash
# dirtest.bash - Demo script by nixCraft under GPL v2.x+
# -------------------------------------------------------
dir="$1"
 
[ $# -eq 0 ] && { echo "Usage: $0 dir-name"; exit 1; }
 
if [ -d "$dir" -a ! -h "$dir" ]
then
   echo "$dir found and setting up new Apache/Lighttpd/Nginx jail, please wait..."
   # __WWWJailSetup "cyberciti.biz" "setup"
else
   echo "Error: $dir not found or is symlink to $(readlink -f ${dir})."
fi

Save and run it as follows:

$ chmod +x dirtest.bash
$ ./dirtest.bash
$ ./dirtest.bash /home/httpd
$ ./dirtest.bash /var/www

Sample outputs:

Fig.01: Shell script in action

Fig.01: Shell script in action


In this example, create directories if does not exists:

# Purpose: Setup jail and copy files
# Author: nixCraft <www.cyberciti.biz> under GPL v2.x+
# Category : Core
# Override : No
# Parameter(s) : d => domain name
#                action => setup or update
__WWWJailSetup(){
        local d="$1"
        local action="${2:setup}"       # setup or update???
        local index="<html><head><title>$d</title></head><body><h1>$d</h1></body></html>" # default index.html
        local J="$(_getJailRoot $d)/$d" # our sweet home 
        local _i=""
 
        [ "$action" == "setup" ] && echo "* Init jail config at $J..." || echo "* Updating jail init config at $J..."
        __init_domain_config "$d"
 
        [ "$action" == "setup" ] && echo "* Setting up jail at $J..." || echo "* Updating jail at $J..."
        [ ! -d "$J" ] &&  $_mkdir -p "$J"
 
        for _i in $J/{etc,tmp,usr,var,home,dev,bin,lib64}
        do
                [ ! -d "$_i" ] &&  $_mkdir -p "$_i"
        done
        for _i in $_lighttpd_webalizer_base/$d/stats/{dump,out}
        do
                [ ! -d "$_i" ] &&  $_mkdir -p "$_i"
        done
        for _i in $_lighttpd_webalizer_prepost_base/$d/{pre.d,post.d}
        do
                [ ! -d "$_i" ] &&  $_mkdir -p "$_i"
        done
## truncated 
}

Summary

Use the following to check file/directory types and compare values:

  1. -L "FILE" : FILE exists and is a symbolic link (same as -h)
  2. -h "FILE" : FILE exists and is a symbolic link (same as -L)
  3. -d "FILE" : FILE exists and is a directory
  4. -w "FILE" : FILE exists and write permission is granted
See also
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 3 comments… read them below or add one }

1 Gilles April 14, 2013 at 8:32 pm

I sometime use “[ -x $dir/. ]“, to check at the same time if directory exist and if the calling process has traverse rights on it.

Reply

2 chukaman August 23, 2013 at 6:18 am

“exits” “exits” “exits” all over the place where you had intended to write “exists”. you might want to change that.

Reply

3 nixCraft August 23, 2013 at 12:08 pm

Thanks for the heads up!

Reply

Leave a Comment

Tagged as: , ,

Previous Faq:

Next Faq: