Site icon DesignLinux

How to Check if a File or Directory Exists in Bash

How to Check if a File or Directory Exists in Bash

While working with Bash and shell scripts, you might need to check whether a directory or a file exists or not on your filesystem.

In order to check whether a file or a directory exists with Bash, you can use test command.

Following is the basic syntax for the test command:

test EXPRESSION
[ EXPRESSION ]
[[ EXPRESSION ]]

Check If File Exists

To check if file exists, you have to use -f FILE operator. It will return true only if the FILE is a regular file.

if [[ -f <file> ]]
then
    echo "<file> exists on your filesystem."
fi

For example, you want to check if the file /etc/passwd exists on your file system or not, you can check by any of the following stuff:

if test -f "/etc/passwd"; then
    echo "File is exists on your system."
fi
if [ -f "/etc/passwd" ]; then
    echo "File is exists on your system."
fi
if [[ -f "/etc/passwd" ]]; then
    echo "File is exists on your system."
fi

If you want to perform a different action based on whether the file exists or not simply use the if/then construct:

if [ -f "/etc/passwd" ]; then
    echo "File is exists on your system."
else 
    echo "File is NOT exists on your system."
fi

Check File Existence using shorter forms

You can also check file existence without using the if statement. If exist status true then after the && operator will be executed.

test -f /etc/passwd && echo "This file exists."
[ -f /etc/passwd ] && echo "This file exists."
[[ -f /etc/passwd ]] && echo "This file exists."

Check if Directory Exist

To check whether directory is exist or not, you have to use -d operator.

Following is the basic syntax for check directory exist:

if [[ -d "$DIRECTORY" ]]
then
    echo "$DIRECTORY exists on your filesystem."
fi

For instance, to check /etc directory exist on your system, you would run:

if [[ -d /etc ]]
then
    echo "/etc exists on your filesystem."
fi

Check if Multiple Files Exist

Sometimes, you have required to check multiple files exists on your system or not. You can do it using && operator.

if [ -f /etc/resolv.conf -a -f /etc/hosts ]; then
    echo "Both files exist."
fi
if [[ -f /etc/resolv.conf && -f /etc/hosts ]]; then
    echo "Both files exist."
fi

Equivalent variants without using the IF statement:

[ -f /etc/resolv.conf -a -f /etc/hosts ] && echo "Both files exist."
[[ -f /etc/resolv.conf && -f /etc/hosts ]] && echo "Both files exist."

Check If File Does Not Exist

You also can check reverse condition for does not exists using the ! (exclamation mark) logical not operator:

if [ ! -f "/etc/passwd" ]; then
    echo "This file does not exist."
fi

In short form you can do as below:

[ ! -f /etc/passwd ] && echo "This file does not exist."

File test operators

Following are the FILE operators that allow you to test for particular types of files:

Conclusion

You learned how to check if a file or directory exists in Bash.

If you have any questions or feedback, feel free to leave a comment.

Exit mobile version