How to Check if a File Exists in Bash?

A bash script contains the commands that are written to perform a specific task in Linux. While writing the code in a bash script, you need to perform the task based on the existence of that file or directory in the system and don’t know how to do it. This guide will describe different ways to check if a file exists using bash/shell scripting. Let’s understand different ways to check the file in bash using the following topics:

Let’s begin!

How to Check if a File Exists in Bash?

The bash script uses several ways to check if the file exists. The test command in bash is utilized to check the existence of the bash file and its type. There are different syntaxes for the test command that are listed below:

$ test EXPRESSION
$ [EXPRESSION]
$ [[EXPRESSION]]

The first syntax is a common way to check the existence of the file in Linux with the test command and the bash script expression. Other ways to find the file’s existence are using the test command, with the single square bracket “[”that is the old format, while the most newer versions use the double square bracket “[[” for bash scripts.

Let’s discuss the options in the test command:

-fReturns true if it is a normal (regular) file.
-eReturns true if it is a regular file, directory, or symbolic link.
-dThis option returns true if the file is a directory.
-hIts output is true if the associated file is a symbolic link.

Let’s use these options to check the files in the system.

How to Check if Single File Exists in Bash?

Let’s perform the above three mentioned syntaxes to check the existence of a single file in the system.

Example 1: Using the test [EXPRESSION]

The easiest way to check the file’s existence is by using the “test” command with the specified expression. The “f” options allow us to check for the regular files in the system. For instance, to check if the “newFile.txt” file is present in the system, we use the below-mentioned bash script expression that displays the “File Exists” string if the file exists in the system:

#!/bin/bash
test -f newFile.txt && echo "File Exists"

In the script, the “test” command for file (f) named “newFile.txt”, and it will the output “Files Exists”. The inside view of the script is shown below:

Let’s run the above bash script in the terminal using the “bash <filename>” command and display the file’s content with the “cat” command as given below:

$ cat testscript.sh
$ bash testscript.sh

The output displays the bash file named “testfile.sh” in the system.

Example 2: Using the [EXPRESSION]

The “f” option with the file name enclosed in the square brackets tells us if the file exists in bash. The “newfile.txt” can be checked and showed the “File Exists” status (if it exists) on output using this command:

#!/bin/bash
[ -f newFile.txt ] && echo "File Exists."

Let’s get the content of the script using “cat” and execute it via “bash”:

$ cat testscript.sh
$ bash testscript.sh

The output displays the “File Exists” text which means the “newFile.txt” exists in the system.

Example 3: Using the [[EXPRESSION]]   

The modern way to check the file in bash is using the expression within the double square brackets [[. For finding the presence of “newFile.txt” in the system, execute the below-mentioned command in the terminal:

#!/bin/bash 
[[ -f newFile.txt ]] && echo "File exist."

The script can be executed as follows:

$ cat testscript.sh
$ bash testscript.sh

The “File exist” status shows the system’s checked file “testscript.sh” exists.

Example 4: Using the if test EXPRESSION

The “if” operator can be utilized with the test command to find the presence of a specific file. Let’s understand the below script:

The below command represents the bash script by assigning a variable “filename” with the value “newFile.txt”. The “if” condition is applied to check the existence of newFile.txt, and in case the file is in the system, it displays the “newFile.txt exists” at the output:

#!/bin/bash
filename=newFile.txt
if test -f "$filename"; then
    echo "$filename exists."
fi

Execute the script as follows:

$ cat testscript.sh
$ bash testscript.sh

The above picture shows the checked file “newFile.txt” is present in the system.

Example 5: Using the if-else test EXPRESSION

The previous example shows the status only “if” the file is present in the system. To get the status in case of file not exists, use the “else” operator. The below script prints the statuses for file “newFile.txt” existence and not exist in both cases:

#!/bin/bash 
filename=newFile.txt
if test -f "$filename"; then
    echo "$filename exists."
else
   echo "$filename does not exists"
fi

Run the script:

$ cat testscript.sh
$ bash testscript.sh

The status “newFile.txt exists” confirms the presence of the file.

Example 6: Using the if [EXPRESSION]

We can use the “if” operator with the [EXPRESSION] syntax to find the presence of the file. To check the file named “newFile.txt”, create this script:

#!/bin/bash 
filename=newFile.txt
if [ -f "$filename" ]; then
    echo "$filename exists."
fi

Execution of the script is carried out as:

$ cat testscript.sh
$ bash testscript.sh

The output verifies the existence of the file.

Example 7: Using the if [[EXPRESSION]]

The double squares brackets “[[“ is utilized to check a single file “newFile.tx” with the below bash script:

#!/bin/bash 
filename=newFile.txt
if [[ -f "$filename" ]]; then
    echo "$filename exists."
fi

The script is executed as:

$ cat testscript.sh
$ bash testscript.sh

The output displays the file “newFile.txt” exists in the system.

How to Check if Multiple Bash Files Exist?

Several files can be checked in the bash script using different syntaxes. Let’s perform it!

Example 1: Using the if [EXPRESSION]

The “a” (all) option is used for checking if multiple files exist with a single command. The below script will check for the “newFile.txt” & “/etc/hosts” files and display the status “Both files exist” only if both files are present in the system:

#!/bin/bash 
if [ -f newFile.txt -a -f /etc/hosts ]; then
    echo "Both files exist."
fi

Let’s execute the script:

$ cat testscript.sh
$ bash testscript.sh

The output indicates the existence of both files.

Example 2: Using the if [[EXPRESSION]]

The usage of “[[” instead of “[” in the above example also checks multiple files at once. The below bash/shell script checks two files, “newFile.txt” and “/etc/hosts” and prints the “Both files exist” status if the two files exist; otherwise, it is left blank:

#!/bin/bash 
if [[ -f newFile.txt && -f /etc/hosts ]]; then
    echo "Both files exist."
fi

The script can be executed as follows:

$ cat testscript.sh
$ bash testscript.sh

The output indicates the existence of both files.

Example 3: Using the [EXPRESSION]

The below scripts execute to check the existence of the two files “newFile.txt” and “/etc/hosts”:

#!/bin/bash 
[ -f newFile.txt -a -f /etc/hosts ] && echo "Both files exist."

Use the below-stated command to run the script:

$ cat testscript.sh
$ bash testscript.sh

The output indicates the existence of both files.

Example 4: Using the [[EXPRESSION]]

To check multiple files (newFile.txt and /etc/hosts) with the “[[” syntax, utilize this command:

#!/bin/bash 
[[ -f newFile.txt && -f /etc/hosts ]] && echo "Both files exist."

The script is run as follows:

$ cat testscript.sh
$ bash testscript.sh

The output indicates the existence of both files.

How to Check Whether a File or a Directory in Bash?

The bash script can check the existence of the file and if the file is a directory or not from the bash script. Let’s do it with different methods.

The “-d” (directory) option allows the user to check if the bash file exists as a directory. The below script shows the status of the file “TestFolder” is a directory otherwise, nothing:

#!/bin/bash 
[ -d TestFolder ] && echo "File is a directory."

Run the script via the command:

$ cat testscript.sh
$ bash testscript.sh

The status confirms that “TestFolder” exists and is a directory.

Another way to check the bash script file is checking for a directory “TestFolder”; if “Yes” then output the status as shown below:

#!/bin/bash
directoryname=TestFolder
if [ -d "$directoryname" ]; then
    echo "$directoryname is a directory."
fi

The execution of the script is as follows:

$ cat testscript.sh
$ bash testscript.sh

The above picture shows “TestFolder” is a directory.

How to Check File/Directory Exists as a Symbolic Link?

We can check if the existing file/directory is a symbolic link or a normal file in bash using several ways. Let’s check it using different examples.

Example 1: Check File Exists & a Symbolic Link

To check if the file “symFile” exists in the system and is a symbolic link (Navigates the user to the original file), the “h” option is utilized; use this bash script:

#!/bin/bash
[ -h symFile.txt ] && echo "File is a symbolic link."

The script’s execution is carried out as:

$ cat testscript.sh
$ bash testscript.sh

The output verifies the “symFile.txt” is a symbolic link.

Similarly, we can use the below bash script code to find if the “symFile.txt” exists and is a symbolic link file. Suppose both the conditions (Exists & Directory) are true. In that case, it will show the status “symFile is a symbolic link.” on the output:

#!/bin/bash
Filename=symFile.txt
if [ -h "$Filename" ]; then
    echo "$Filename is a symbolic link."
fi

The script is executed via the command:

$ cat testscript.sh
$ bash testscript.sh

The “symFile.txt” is a symbolic link of a file as shown in the above picture.

Example 2: Check Directory Exists & a Symbolic Link

We can check for a directory (symFolder) if it exists and is a symbolic link using the “h” option, executing this command:

#!/bin/bash
[ -h symFolder ] && echo "Directory is a symbolic link."

Let’s execute the script:

$ cat testscript.sh
$ bash testscript.sh

Based on the output, the directory appears to be a symbolic link.

Another way to find the existence of the directory and a symbolic link for “symFolder”, utilize the below command:

#!/bin/bash
directoryname=symFolder
if [ -h "$directoryname" ]; then
    echo "$directoryname is a symbolic link."
fi

Now, execute the script:

$ cat testscript.sh
$ bash testscript.sh

That’s the end of this post.

Conclusion

Three different methods are used to check if a file exists in bash, which are executed using the “test EXPRESSION”, [EXPRESSION], and [[EXPRESSION]] commands. Moreover, these commands use the if-else operator to print the status of specific existing and non-existing files. This guide has all the necessary details to check if a file exists in bash.