Skip to main content
Bash

How to Check if File or Directory Exists in Bash Shell

Here are several ways you can check if file or directory exists in bash shell script. You'll also learn to check if file doesn't exist.

Abhishek Prakash

If you are working on a Bash script that interacts with files and directories, you might encounter a situation where you need to make sure that the file or directory exists. This helps avoiding possible errors for performing certain actions on a file that doesn’t exist.

In this tutorial, I’ll show you a couple of ways to check if file or directory exists in bash script or not. Let’s start with file first.

Check if file exists in bash script

The idea here is to use the -f operator that returns true only when it is a regular file (not directory).

Let’s say you want to check if file /home/user/my_file exists or not. Here’s how you can check with square brackets

#!/bin/bash
 
if [ -f /home/user/my_file ]
then
    echo "My file exists"
fi

But you won’t always get the file name before hand, will you? You can have it in a variable and if that’s the case, you can use it in this fashion.

#!/bin/bash

FILE=/home/user/my_file
 
if [ -f "$FILE" ]
then
    echo "My file exists"
else 
    echo "My file doesn't exist"
fi

Basically, what matters is the condition you use in the if command. It’s up to you how you want to use the if statement.

For example, you can write it with two square brackets, keep ‘then’ in the same line as ‘if’ with the help of semicolon like this:

#!/bin/bash

FILE=/home/user/my_file
 
if [ -f "$FILE" ]; then
    echo "My file exists"
else 
    echo "My file doesn't exist"
fi

or put the entire statement together like this:

[ -f /home/user/my_file ] && echo "My file exists" || echo "My file doesn't exist"

Check file exists in bash with test

You can also use test in bash to see if file exists or not. It’s pretty much the same thing only that you don’t use the square brackets in the if statement:

#!/bin/bash

FILE=/home/user/my_file
 
if test -f "$FILE" 
then
    echo "My file exists"
else 
    echo "My file doesn't exist"
fi

You can also use the above code in single line like this:

test -f /home/user/my_file && echo "My file exists" || echo "My file doesn't exist"

Check if file doesn’t exist in bash script

What if it’s the other way round and you want to check if file does not exist in bash? You can use pretty much the same code as above by using the negation operator:

#!/bin/bash

FILE=/home/user/my_file
 
if [ ! -f "$FILE" ]
then
    echo "My file doesn't exist"
fi

Now that you know how to deal with files, let’s move on to directories.

Check if directory exists in bash script

The code for checking directory is the same as the one you saw in the previous section. The only difference is that you’ll be using -d instead of -f. -d returns true only for directories.

#!/bin/bash
 
if [ -d /home/user/my_dir ]
then
    echo "My directory exists"
fi

You can also use test here:

#!/bin/bash

DIR=/home/user/my_dir
 
if test -d "$DIR" 
then
    echo "My directory exists"
else 
    echo "My directory doesn't exist"
fi

Check if directory doesn’t exist in bash

You can use the negation again to check if directory doesn’t exist:

#!/bin/bash

DIR=/home/user/my_dir
 
if [ ! -d "$DIR" ]
then
    echo "My directory doesn't exist"
fi

That’s it. That’s all you need to do for checking if a directory of file exists in bash shell or not.

I hope you find this bash tip useful. If you have any questions or suggestions, please feel free to leave a comment below.

Abhishek Prakash