Bash Script to Check if File is Directory

Bash Script to Check if File is Directory – To check if a specified path exists and is a directory in bash scripting, use the [ -d FILE ] test expression with a bash if statement. The -d file test returns true when the path exists and refers to a directory.

This tutorial explains the basic directory check, how to pass a path to a function, how to use the test safely with variables, and how -d differs from common file tests such as -e and -f.

Bash directory test syntax using -d

The common syntax for checking whether a path is a directory is shown below. Quote the variable or path value so that spaces, empty values, and special characters do not break the test expression.

</>
Copy
if [ -d "$path" ]; then
    echo "$path is a directory"
else
    echo "$path is not a directory"
fi

You may also use the Bash keyword form [[ -d "$path" ]]. Both forms are commonly used in shell scripts. The [[ ... ]] form is Bash-specific, while [ ... ] is widely used in POSIX-style shell examples.

Example 1 – Check if File Exists And is a Directory

We shall consider /home/tutorialkart/ and /home/tutorialkart/sample.txt which are directory and file respectively. We shall verify the same with the help of following bash script.

Bash Script File

</>
Copy
#!/bin/bash

# Scenario - File exists and is a directory
if [ -d /home/tutorialkart/ ];
then
	echo "/home/tutorialkart is directory"
else
	echo "/home/tutorialkart is not a directory"
fi

# Scenario - File exists and is not a directory
if [ -d /home/tutorialkart/sample.txt ];
then
	echo "/home/tutorialkart/sample.txt is a directory"
else
	echo "/home/tutorialkart/sample.txt is not a directory"
fi

When the above bash shell script is run in Terminal, we will get the following output.

Output

$ ./bash-script-if-file-is-directory 
/home/tutorialkart is directory
/home/tutorialkart/sample.txt is not a directory

The first condition is true because /home/tutorialkart/ is a directory. The second condition is false because /home/tutorialkart/sample.txt is a regular file, not a directory.

Example 2 – Check if Path is Directory

We shall consider the same file and directory mentioned in the previous example. In this example, we shall write a function whose first argument is a FILE. And in the function we shall check if the passed argument (FILE) exists and is a directory.

Bash Script File

</>
Copy
#!/bin/bash

# function to check if passed argument is a directory and exists
checkIfDirectory() {
	# $1 meaning first argument
	if [ -d "$1" ];
	then
		echo "$1 exists and is a directory."
	else
		echo "$1 is not a directory."
	fi
}

# Scenario - File exists and is a directory
checkIfDirectory "/home/tutorialkart/"

# Scenario - File exists and is not a directory
checkIfDirectory "/home/tutorialkart/sample.txt"

When the above script is run in Terminal, we will get the following output.

Output

$ ./bash-script-if-file-is-directory-2 
/home/tutorialkart/ exists and is a directory.
/home/tutorialkart/sample.txt is not a directory.

Check if a command-line argument is a directory in Bash

In practical scripts, the directory path is often passed as an argument. In that case, validate that an argument was provided before applying the -d test. This gives a clearer message when the user runs the script without a path.

</>
Copy
#!/bin/bash

path="$1"

if [ -z "$path" ]; then
    echo "Usage: $0 DIRECTORY_PATH"
    exit 1
fi

if [ -d "$path" ]; then
    echo "$path is a directory"
else
    echo "$path is not a directory"
fi

Example runs for a directory, a regular file, and a missing argument are shown below.

$ ./check-directory.sh /home/tutorialkart
/home/tutorialkart is a directory

$ ./check-directory.sh /home/tutorialkart/sample.txt
/home/tutorialkart/sample.txt is not a directory

$ ./check-directory.sh
Usage: ./check-directory.sh DIRECTORY_PATH

Difference between -d, -e, and -f in Bash

The -d test is used when you specifically want to know whether a path is a directory. Other file tests answer different questions, so choose the operator based on what the script needs to verify.

Bash testMeaningUse it when
[ -d "$path" ]True if the path exists and is a directory.You need a folder path for reading, writing, scanning, or creating files inside it.
[ -e "$path" ]True if the path exists.You only need to know whether anything exists at that path.
[ -f "$path" ]True if the path exists and is a regular file.You need a normal file, such as a text file, log file, or configuration file.

For example, a path can pass -e but fail -d when it exists as a regular file. That is why checking only for existence is not enough when your script requires a directory.

Check if a Bash directory is readable or writable

Sometimes a script needs more than directory existence. For example, a backup script may need a writable target directory, and a cleanup script may need permission to read a directory. You can combine -d with permission tests such as -r and -w.

</>
Copy
#!/bin/bash

dir="$1"

if [ ! -d "$dir" ]; then
    echo "$dir is not a directory"
    exit 1
fi

if [ ! -r "$dir" ]; then
    echo "$dir is a directory, but it is not readable"
    exit 1
fi

if [ ! -w "$dir" ]; then
    echo "$dir is a directory, but it is not writable"
    exit 1
fi

echo "$dir is a readable and writable directory"

Common mistakes while checking a directory in Bash

  • Not quoting the path variable: Use [ -d "$path" ], not [ -d $path ]. Quoting avoids errors when the path contains spaces or is empty.
  • Using -e when the script requires a directory: -e only checks whether a path exists. It does not confirm that the path is a directory.
  • Confusing a regular file with a directory: Use -f for regular files and -d for directories.
  • Skipping argument validation: If the script expects a directory path as $1, check that the argument is not empty before testing it.
  • Assuming permissions are available: A path may be a directory but still not be readable or writable by the current user.

QA checklist for a Bash script that checks if a path is a directory

  • Use -d when the script specifically needs a directory path.
  • Quote every variable inside the test expression, for example "$dir".
  • Handle empty command-line arguments with a usage message.
  • Test the script with an existing directory, a regular file, and a path that does not exist.
  • Add permission checks such as -r or -w if the script must read from or write to the directory.

FAQs on Bash script to check if file is directory

How do I check if a path is a directory in Bash?

Use [ -d "$path" ] in an if statement. The condition is true when the path exists and is a directory.

Does -d also check whether the directory exists?

Yes. The -d test returns true only when the path exists and is a directory. If the path does not exist, the condition is false.

What is the difference between -d and -f in Bash?

-d checks for a directory, while -f checks for a regular file. A folder passes -d, and a normal text file passes -f.

Why should I quote the variable in [ -d “$dir” ]?

Quoting the variable prevents syntax errors and incorrect results when the directory path contains spaces or when the variable is empty.

Can I use [[ -d “$dir” ]] instead of [ -d “$dir” ]?

Yes, in Bash scripts you can use [[ -d "$dir" ]]. The single-bracket form is common in shell examples, while the double-bracket form is a Bash keyword with Bash-specific behavior.

Conclusion: using -d to confirm a Bash directory path

In this Bash Tutorial – Bash Script to Check if File is Directory, we have used the -d test expression to check whether a specified path exists and is a directory. For safer scripts, quote path variables, validate command-line arguments, and add read or write permission checks when the script needs access to the directory contents.