BASH Programming

Check If the File Exists in Bash

Different types of files are used in Bash for different purposes. Many options are available in Bash to check if the particular file exists or not. The existence of the file can be checked using the file test operators with the “test” command or without the “test” command. The purposes of different types of file test operators to check the existence of the file are shown in this tutorial.

File Test Operators

Many file test operators exist in Bash to check if a particular file exists or not. Some of them are mentioned in the following:

Operator Purpose
-f It is used to check if the file exists and if it is a regular file.
-d It is used to check if the file exists as a directory.
-e It is used to check the existence of the file only.
-h or -L It is used to check if the file exists as a symbolic link.
-r It is used to check if the file exists as a readable file.
-w It is used to check if the file exists as a writable file.
-x It is used to check if the file exists as an executable file.
-s It is used to check if the file exists and if the file is nonzero.
-b It is used to check if the file exists as a block special file.
-c It is used to check if the file exists as a special character file.

Different Examples to Check Whether the File Exists or Not

Many ways of checking the existence of the regular file are shown in this part of the tutorial.

Example 1: Check the Existence of the File Using the -F Operator with Single Third Brackets ([])

Create a Bash file with the following script that takes the filename from the user and check whether the file exists in the current location or not using the -f operator in the “if” condition with the single third brackets ([]).

#!/bin/bash

#Take the filename

echo -n "Enter the filename: "

read filename

#Check whether the file exists or not using the -f operator

if [ -f "$filename" ]; then

echo "File exists."

else

echo "File does not exist."

fi

The script is executed twice in the following script. The non-existence filename is given in the first execution. The existing filename is given in the second execution. The “ls” command is executed to check whether the file exists or not.

Example 2: Check the Existence of the File Using the -F Operator with Double Third Brackets ([[ ]])

Create a Bash file with the following script that takes the filename as a command-line argument and check whether the file exists in the current location or not using the -f operator in the “if” condition with the double third brackets ([[ ]]).

#!/bin/bash

#Take the filename from the command-line argument

filename=$1

#Check whether the argument is missing or not

if [ "$filename" != "" ]; then

#Check whether the file exists or not using the -f operator

if [[ -f "$filename" ]]; then

echo "File exists."

else

echo "File does not exist."

fi

else

echo "Argument is missing."

fi

The script is executed twice in the following script. No argument is given in the first execution. An existing filename is given as an argument in the second execution. The “ls” command is executed to check whether the file exists or not.

Example 3: Check the Existence of the File Using the -F Operator with the “Test” Command

Create a Bash file with the following script that takes the filename as a command-line argument and check whether the file exists in the current location or not using the -f operator with the “test” command in the “if” condition.

#!/bin/bash

#Take the filename from the command-line argument

filename=$1

#Check whether the argument is missing or not

if [ $# -lt 1 ]; then

echo "No argument is given."

exit 1

fi

#Check whether the file exists or not using the -f operator

if test -f "$filename"; then

echo "File exists."

else

echo "File does not exist."

fi

The script is executed twice in the following script. No argument is given in the first execution. An existing filename is given in the second execution.

Example 4: Check the Existence of the File with the Path

Create a Bash file with the following script that checks whether the file path exists or not using the -f operator with the “test” command in the “if” condition.

#!/bin/bash

#Set the filename with the directory location

filename='temp/courses.txt'

#Check whether the file exists or not using the -f operator

if test -f "$filename"; then

echo "File exists."

else

echo "File does not exist."

fi

The following output appears after executing the script:

Conclusion

The methods of checking whether a regular file exists or not in the current location or the particular location are shown in this tutorial using multiple examples.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.