Bash - Check if file exists

There are several ways to check the availability of a file in Linux. The “test” command in bash scripting is one of the critical approaches to checking a file’s existence. The focus of this guide is to discuss the presence of a file in your system through bash scripting:

How to check file existence using bash scripting:

1) By entering the file name in the terminal:

Firstly, we need to create a bash script file, use the below-mentioned command:touch testfile.sh.

The name of the file I created is “testfile.sh”, the “.sh” extension indicates the shell script file:

Open the “testfile.sh” in any text editor. Then write the script, and save it by pressing “save.”

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

Use “-f” to check the file’s existence.

Write the below script:

#!/bin/bash
echo “Enter your filename”
read newfile1
if [ -f “$newfile1” ]
then
echo “File is found”
else
   echo “File is not found”
fi

Go back to the terminal and run the file to print output:./filename.sh

Permission denied message would be displayed in the terminal.

Make it executable by executing the below-mentioned command:$chmod +x testfile.sh

Enter the file name, and it will print the output: “File is found”

2) By entering the file name while writing the script:

Another way to find a file is by giving the file name while writing the script. We have three ways to check the availability of the file. The first one is using the “test” command, the second is using “if” with an expression in square brackets, and the third is also with “if” but double square brackets as indicated below:

  1. “test EXPRESSION.”
  2. “if [ EXPRESSION ]”
  3. “if [[ EXPRESSION ]]”

Let’s understand it with examples:

test [ Expression ]

Copy the given script and paste it into the editor; save it:

#!/bin/bash
filename=file1
if test -f “$filename”;
then
    echo “$file has found.”
else
    echo “$file has not been found”
fi

Output: “file1 has not been found”

As there is no such file in my directory, therefore the code displays the “File is not found” message.

if [ Expression ]

Copy the following script to check the if file exists or not:

#!/bin/bash
filename=myfile.txt
if [ -f “$filename” ];
then
    echo “$filename has found.”
else
    echo “filename has not been found”
fi

Output: “myfile.txt has found.”

if [[ Expression ]]

Copy the below-written script and paste it on the terminal:

#!/bin/bash
filename=testfile
if [[ -f “$filename” ]];
then
    echo “$filename has found.”
else
    echo “$filename has not been found”
fi

Output: “testfile has not found.

To check the directory:

Use the “-d” flag to check the existence of a directory.

In the below-mentioned script, “dir11” is the variable in which you store the file the one you are finding; in this example, I want to check directory name “testDir” exists or not.

#!/bin/bash
dir11=testDir
if [ -d “$dir11” ]
then
echo “Directory has found”
else
   echo “Directory has not been found”
fi

Output: “Directory has not been found”

Enter the file name in the terminal:

When you run the command in the terminal to check whether the directory exists or not, you are required to enter the directory name that you are searching for:

#!/bin/bash
echo “type your directory name.”
read Dir1
if [ -d “Dir1” ]
then
echo “directory has been found”
else
   echo “directory has not found”
fi

Output: “directory has not found”

Checking the file without using the “if” statement:

The “test” command can be executed without the “if” statement. It will only display output if the file exists; else, there would be no output:

Write script:

  1. test -f myfile.txt && echo “file has been found”

Output: “file has been found”

  1. [ -f myfile.txt ] && echo “$file has been found.”

Output: “file has been found”

  1. [[ -f myfile.txt ]] && echo “$file has been found.”

Output: “file has been found”

Output: “file has been found”

Checking the directory without using the “if” statement:

Use the below-mentioned statements to check a directory exists or not:

  1. [[ -d testDir ]] && echo “directory does exist”
  2. 2) [ -d testDir ] && echo “directory does exist”

Output: “directory does exist”

Checking multiple files/Directories:

1) Checking multiple files with “if” statements:
Use the “-a” flag to check the existence of various files instead of using nested “if/else” statements:

#!/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: “Both files exist.”

Checking multiple files without using the “if” statement:

Use the following statement to check multiple files simultaneously without using “if”

[[ -f new_file.txt  && -f  newfile.txt ]] && echo “Both files exits.”

Output: “Both files exist.”

[[ -f new_file.txt  && -f  newfile.txt ]] && echo “Both files exits.”

Output: “Both files exist.”

Conclusion:

This article has shown how to check a file or a directory through bash scripting. We used different options to check the availability of a file. Firstly, we use the “test” command with different flags. Then we learned the usage of “if”, nested “if-else,” and without the “if” statements to check the file or directory. We also looked over how to check multiple files or directories.