BASH Programming

How to Check if a File Exists in Bash?

In Linux, everything is saved in a file. To check if a file exists and if it’s a standard one, use the test command with the -f option. Similarly, using the test command with the -d operator helps you to see if a file or directory exists.

In this guide, we will learn how to use a Bash script to check if a file or directory exists.

Create File

We need to create a file using the below-given command:

$ touch newfile.sh

To create a file we use the touch command and “.sh” is the bash file extension.
D:\Aqsa\18 march\how to check in bash if file exists\how to check in bash if file exists\images\image5 final.png

Open the file and write the script, then save the text by pressing “save”.

One way is to find a file by asking for a filename from the user on a terminal.

Use “-f” to check the file.

Write the below script:

#!/bin/bash
echo "Enter filename"
read newfile1
if [ -f "$newfile1" ]
then
echo "File exists"
else
   echo "File does not exist"
fi

Go back to the terminal and run the file to print output:

./filename.sh

Permission denied message will be displayed in the terminal.

D:\Aqsa\18 march\how to check in bash if file exists\how to check in bash if file exists\images\image6 final.png

Make it executable using the following command:

$ chmod +x newfile.sh

Enter the file name, and the output will be printed:

$ ./newfile.sh

D:\Aqsa\18 march\how to check in bash if file exists\how to check in bash if file exists\images\image8 final.png

One way is to find a file by telling the file name while writing code.

We have three ways to write a statement:

Test EXPRESSION

[ EXPRESSION ]

[[ EXPRESSION ]]

If [ -f “$filename” ]

If test –f “$fiename.”;

If [[ -Expression ]] ;

Output:

D:\Aqsa\18 march\how to check in bash if file exists\how to check in bash if file exists\images\image12 final.png

Without using if

The test command can also be used without an if statement. Only if the test command’s exit status is valid, the command after the && operator will be executed.

Output:

D:\Aqsa\18 march\how to check in bash if file exists\how to check in bash if file exists\images\image21 final.png

Check Multiple Files

Instead of complicated nested if/else constructs, use -a (or && with [[) to check if multiple files exist:

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

Another way is:

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

Output:

D:\Aqsa\18 march\how to check in bash if file exists\how to check in bash if file exists\images\image2 final.png

Without Using IF Statement

Use the following syntax to check file without using IF:

[ -f file1_name –a –f file2_name ] && echo “Print statement”

[ -f file1_name <strong> &&</strong> -f file2_name ] && echo “Print statement.”

Conclusion:

This article has shown how to use Bash to search and find if whether a file or directory exists.

If else is used to perform different actions based on the value of the expression used to evaluate whether or not a file or directory exists. Create a nested if-else statement and check the file or directory without using the IF statement.

About the author

Avatar

Aqsa Maqbool

As a Software engineer, I am passionate to write about various IT related
articles but have deep interest in Linux. I spend most of my time reading Linux related blogs and IT related books. I want to serve the world with my writing skills.