Bash Check If File Exists

Bash Check File If Exists

When working with files in bash, it is essential to know whether the particular file or directory exists. Based on the availability of the files and directories, you can perform further operations and tasks. This tutorial will introduce, explain, and demonstrate how to check if a file or directory exists in the specified location. Let’s explore them.

the test Command

The test command is a Unix-based utility used to check file types and compare values. You can check if the file exists using the test command with these functionalities. The syntax of the command is as follows.

test EXPRESSION

Here, EXPRESSION is the expression to be evaluated.

The syntax also takes the other forms where the command test is excluded.

[ EXPRESSION ]
[[ EXPRESSION ]]

The single bracket, [ around the expression is the older, compatible and bash built-in syntax, while the double brackets, [[ is a modern one, which may not be compatible with all the shells.

The exit status of the command is determined by the EXPRESSION. If it is evaluated as true, the exit status is 0, and if it is false, the exit status is 1.

The EXPRESSION can take the following forms while being evaluated.

  • (EXPRESSION): EXPRESSION is evaluated true.
  • !EXPRESSION: EXPRESSION is evaluated false.
  • EXPRESSION1 -a EXPRESSION2: the exit status is true both the expressions are true.
  • EXPRESSION1 -o EXPRESSION2: the exit status is true even if only one of the expressions is true.

test File Operators

The test command takes the following file operators that provide the following functionalities while working with files.

  • -f FILE: returns true if FILE exists and it is a regular file.
  • -d FILE: returns true if FILE exists and it is a directory.
  • -e FILE: returns true if FILE exists, nevertheless of its type. It includes any kind of files like nodes, devices, directories, etc.
  • -b FILE: returns true if FILE exists and it is a special block file.
  • -O FILE: returns true if FILE exists and the current user owns the file.
  • -r FILE: returns true if FILE exists and it has the read permission.
  • -w FILE: returns true if FILE exists and it has the write permission.
  • -x FILE: returns true if FILE exists and it has the execute permission.
  • -S FILE: returns true if FILE exists and it is a socket.

Use test Command to Check if File Exists

You have learned the basics of the test command with its syntax and operators. In the sub-sections below, you will discover various ways of checking if the file or the directory exists in the specified location with the things you have learned.

First of all, create a structure of a directory and files to carry the test to check the availability of the files and directories in various scenarios.

The structure looks like this.

myFolder
├── abc.txt
├── def.txt
└── xyz.conf

You can use the mkdir command to create the directory and the touch command to create the file.

Use the test Command With the if Statement to Check if the File Exists

The following example demonstrates the implementation of the syntax test EXPRESSION.

file=myFolder/abc.txt
if test -f $file ; then
    echo "$file exists"
fi
Output
myFolder/abc.txt exists

The following example demonstrates the implementation of the syntax [ EXPRESSION ].

file=myFolder/def.txt
if [ -f $file ] ; then
    echo "$file exists"
fi
Output
myFolder/def.txt exists

The following example demonstrates the implementation of the syntax [[ EXPRESSION ]].

file=myFolder/xyz.conf
if [[ -f $file ]] ; then
    echo "$file exists"
fi
Output
myFolder/xyz.conf exists

In the above examples, the -f operator is used to check if the files are regular. The bash scripts show the files’ existence as they exist in the specified location.

Use the test Command Along With the && Operator to Check if the File Exists

You can also use the test command omitting the if statement to check if the file exists. Instead of the if statement, you can use the && and || operators.

The command on the right side of the && operator will only execute if the operator’s left command evaluates true. The operator || is contrary as the right side of it will only execute if its left is false.

The example below illustrates the implementation of the test command in various syntax forms.

file=myFolder/abc.txt
test -f $file && echo "$file exists"
Output
myFolder/abc.txt exists
file=myFolder/def.txt
[ -f $file ] && echo "$file exists"
Output
myFolder/def.txt exists
file=myFolder/xyz.conf
[[ -f $file ]] && echo "$file exists"
Output
myFolder/xyz.conf exists

Since all the files exist in the specified location, the output sections above show the files exist.

Check if Multiple File Exists

You can use the operator -a between two expressions to check if both the files exists in bash. As discussed above, the whole expression is evaluated as true only if both of the expression used with the -a operator is true.

if test -f myFolder/abc.txt -a -f myFolder/def.txt; then
    echo "both files exits"
fi
Output
both files exits

Here, both the files are present. Therefore, the expression in the if statement is evaluated as true.

The above example can be implemented using the brackets syntax as well. The implementation is done below.

if [ -f myFolder/xyz.conf -a -f myFolder/def.txt ]; then
    echo "files exits"
fi
Output
both files exits

You can also use the && operator to check if both the files exist in the specified location. The example is shown below.

[[ -f myFolder/xyz.conf && -f myFolder/abc.txt ]] && echo "both files exists"

Check if File Does Not Exist

You can use the !EXPRESSION form to check if the file does not exist in the specified location.

file=myFolder/logo.png
if test ! -f $file; then
    echo "$file does not exist"
fi
Output
myFolder/logo.png does not exist

Here, the file logo.png is not present inside the myFolder directory. As a result, the bash script above shows that the file does not exist.

The other variant of checking if the file does not exist is shown below.

if [ ! -f myFolder/index.html ]; then
    echo "the file does not exist"
fi
Output
the file does not exist

Check if Directory Exists

You can use the -d operator to check if a directory exists. An example is shown below.

if [ -d myFolder ]; then
    echo "the directory exists"
fi
Output
the directory exists

The example shows that a directory myFolder exists.

Conclusion

Thus, the test command lets you check whether a file or a directory exists. You learned how to use it along with the if statement and the && operator to check the existence of a file or a directory.

 

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
Bash Printf Command
Read More

Bash Printf Command

The bash printf command prints the formatted strings in the shell. It stands for print formatted. The command…