Linux / UNIX: Find Out If a Directory Exists or Not

I’ve already written a small tutorial about finding out if a file exists or not under Linux / UNIX bash shell. However, couple of our regular readers like to know more about a directory checking using if and test shell command.

General syntax to see if a directory exists or not

[ -d directory ]
test directory
See if a directory exists or not with NOT operator:
[ ! -d directory ]
! test directory

Find out if /tmp directory exists or not

Type the following command:
$ [ ! -d /tmp ] && echo 'Directory /tmp not found'
$ [ -d /tmp ] && echo 'Directory found' || echo 'Directory /tmp not found'

Sample Shell Script to gives message if directory exists

Here is a sample shell script:

if [ $# -ne 1 ]
	echo "Usage: $0 {dir-name}"
	exit 1
if [ -d "$DIR" ]
	echo "$DIR directory  exists!"
	echo "$DIR directory not found!"

🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
7 comments… add one
  • Arun pandey Mar 16, 2016 @ 19:42

    # dirtest.bash – Demo script by nixCraft under GPL v2.x+
    # ——————————————————-

    [ $# -eq 0 ] &if [ -d “$dir” -a ! -h “$dir” ]
    echo “$dir found and setting up new Apache/Lighttpd/Nginx jail, please wait…”
    # __WWWJailSetup “” “setup”
    echo “Error: $dir not found or is symlink to $(readlink -f ${dir}).”

  • John McLean Oct 10, 2013 @ 16:32

    easier method:

    ls -d /some/carzy/directory/does/it/exist
    ls: cannot access /some/carzy/directory/does/it/exist: No such file or directory

  • Qrious Nov 21, 2012 @ 11:46

    Helps me a lot !
    Thanks for posting..

  • vignesh Dec 14, 2011 @ 13:15

    i need to print the file names of all files having .txt extension of a given directory after converting to uppercase letters. The input (directory name) should be given as command line argument. The script will also check whether sufficient arguments are passed or not and whether the argument is a directory or not

    i tried using this code

    if [ -f $filename ]
    tr “[a-z]” “[A-Z]” <$filename

    but i dint get output

  • aneesh Nov 18, 2008 @ 5:29

    Dear Friends,

    We can do the same thing to check a file exist or not by modifying the “if” loop like the following.


    if [ $# -ne 1 ]
    echo “Usage: $0 {file-name}”
    exit 1

    if [ -f “$FILE” ]
    echo “$FILE file exists!”
    echo “$FILE file not found!”

    • Vamsi Oct 27, 2010 @ 9:11

      u can use test -f to check whether filename exists or not
      u can use test -d to check whether directoryname exists or not

      Returns “zero” if exists and “one” if doesn’t exist.

      • Raj Shah Aug 28, 2013 @ 11:02

        # [ -f /path/to/dir/file_name ] && echo “File Found” || echo “File not found”

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @