How to check if file exists in bash

Endrit Qerreti

Endrit Qerreti

In this tutorial, you will learn how to check if a file exists on your system in a bash function.

We are going to write a simple function in bash that will check if a file exists, and then display a message, if the file exists or not.

Check if file exists

For example, the code below will check if the file "owlhowto.txt" exists on the same path location with the bash script

if [[ -f "owlhowto.txt" ]];
then
echo " yes, owlhowto.txt exists"
else
echo " no, owlhowto.txt does not exist"
fi

Example Output

Yes, owlhowto.txt exists

To check if your file exists, simply replace owlhowto.txt with your file, you can also use variables, to make it more easy to read and edit.

We are going to declare a variable called file and the value of this variable will be owlhowto.txt, so now instead of checking for our txt file directly, we can check if the variable exists as shown in the code below

file='owlhowto.txt'
if [[ -f "$file" ]];
then
echo " yes, owlhowto.txt exists"
else
echo " no, owlhowto.txt does not exist"
fi

Variable can be called anything you want. However, it's a good coding habit to name variables based on their function.

You can also use the variable file when you echo the output instead of writing manually the file name, in this case owlhowto.txt.

This way you won't need to set the name of file

file='owlhowto.txt'
if [[ -f "$file" ]];
then
echo " yes, $file exists"
else
echo " no, $file does not exist"
fi

So far we covered two methods of checking if a file exists in bash, with and without variables. Let's say you want to include this code in your existing project as a function.

To make this code a function is very easy, all you have to do is declare function and copy the code above into the function, for example

Function to Check if file exists

In bash there are two methods of declaring a function, let's try both methods and see what the difference is.

Method 1, using the word function which is followed by the function name, our function name if called check_file

function check_file(){

}

And call the function

check_file

Method 2, by declaring the function directly, without having to type the function word

check_file(){

}

Then to run this function, you can simply do

check_file

Even though both functions are declared differently, the function name does not change, so to run the function simply call the function name.

The difference between two functions is the reserved word function, both functions are called the same but in the first one we are using the reserved word function, and in the second example not.

Here's the full code

Function without the reserved word function

check_file(){
file='owlhowto.txt'
if [[ -f "$file" ]];
then
echo " yes, $file exists"
else
echo " no, $file does not exist"
fi

}

check_file

Function using the reserved word function

function check_file(){
file='owlhowto.txt'
if [[ -f "$file" ]];
then
echo " yes, $file exists"
else
echo " no, $file does not exist"
fi

}

check_file

Conclusion

In this tutorial, you learned how to check if a file exists in bash. We also explained two methods of declaring a function in bash, using the reserved word function, and using only function without the reserved word.