BASH Programming

Bash Check If File Not Exists

Bash scripting is a programming language that can be used quickly in Linux and other operating system environments to automate your tasks. When writing bash scripts frequently we need to operate on files in the filesystem. However if our code tries to access a file and it does not exist we can get an unexpected error. Therefore its best practice to check for files existence before trying to reference the file in a bash scripts.

Example 01

We have to add the Bash support in it as ‘#!/bin/bash’. After this, we have initialized a ‘FILE’ variable with the path to the file itself ‘file.sh’ i.e., ‘/home/linuxhint/file.sh’.  To check for the file exists or not, we will be using the ‘If-then’ statement taking its conditions in the single square brackets in this Bash code. To check whether the file exists, we need to use the ‘-f’ option followed by the double-quoted variable “FILE” with the dollar character “$”. This is to check whether the given path to the file contains a file with the name ‘file.sh’ or not.

The condition ends here and the ‘then’ part of the statement starts. If the file exists, it will display the message that the file ‘file.sh’ exists in the given path using the ‘echo’ statement. The ‘if’ statement ends here with the ending syntax ‘fi’.

#!/bin/bash
FILE=/home/linuxhint/file.sh
if [ -f "$FILE" ]; then
        echo "$FILE exists!"
fi

Now, save your Bash code file and run file using the command line below:

bash file.sh

Here is the output when running the command:

linuxhint@u22:~$ bash file.sh
/home/linuxhint/file.sh exists!

If we tweak the script and change the ‘FILE’ to a file that doesn’t exist and rerun the script we get no output from the script.

Example 02

In this example we will add an else statement to the if statement in the previous example. That way if the file does not exist we will get an appropriate error message printed to the standard output or screen. Here is the new code:

#!/bin/bash
FILE=/home/linuxhint/file.sh
if [ -f "$FILE" ]; then
        echo "$FILE exists!"
else
        echo "$FILE does not exist!"
fi

We can run the scripts as follows:

$ bash file.sh

We get the same output as before:

linuxhint@u22:~$ bash file.sh
/home/linuxhint/file.sh exists!

But now if we hack the filename to be a file that does not exist we will get a proper error message:

linuxhint@u22:~$ bash file.sh
/home/linuxhint/nofile.sh does not exist!

Example 03

Let’s say, you want to use the ‘not’ character so you are firstly checking if the file does not exist. For example if the file does not exist you may want to print an error message, but you may not even require an else in this context as you only care if the file doesn’t exist, otherwise the script may continue as normal without exiting an error.

Here is the syntax to use the ‘!’ character to reverse the check to check if the file does not exist:

#!/bin/bash
FILE=/home/linuxhint/nofile.sh
if [ ! -f "$FILE" ]; then
        echo "$FILE does not exist!"
else
        echo "$FILE exists!"
fi

We will first run the script with the broken filename:

$ bash file.sh

The output when it triggers the error condition is here below:

linuxhint@u22:~$ bash file.sh
/home/linuxhint/nofile.sh does not exist!

Now we can fix the filename to be the file that exists:

#!/bin/bash
FILE=/home/linuxhint/file.sh
if [ ! -f "$FILE" ]; then
        echo "$FILE does not exist!"
else
        echo "$FILE exists!"
fi

Here is the output with the file that exists:

linuxhint@u22:~$ bash file.sh
/home/linuxhint/file.sh exists!
linuxhint@u22:~$

Example 04

In this example we will show a quick syntax from the shell without writing a script and introduce logical operators to print success or errors. Here is the script you can run on the terminal:

linuxhint@u22:~$ [ -f file.sh ] && echo "file exists" || echo "file doesn't exist"
file exists
linuxhint@u22:~$

In the above code we write the code directly on the interactive terminal. We use a conditional test for checking if the file exists. It will return 0 for success. Logical OR and Logical AND operators are used to indicate which message should be printed, success or failure.

Example 05

You can also utilize a similar approach as shown in this article to check for the existence of a directory using the ‘-d’ option in its condition instead of ‘-f’. Let’s say, we have a directory ‘test’ in our home folder and we have been using the same script with the ‘-d’ option to check for it and display the string message according to the condition output.

Here is the code with the directory check in file ‘dir.sh’:

#!/bin/bash
FILE=/home/linuxhint/test
if [ -d "$FILE" ]; then
        echo "directory $FILE exists!"
else
        echo "directory $FILE does not exist!"
fi

To run the code execute:

$ bash dir.sh
linuxhint@u22:~$ bash dir.sh
directory /home/linuxhint/test exists!

And the negative test code and output:

#!/bin/bash
FILE=/home/linuxhint/test
if [ ! -d "$FILE" ]; then
        echo "directory $FILE does not exist!"
else
        echo "directory $FILE exists!"
fi
linuxhint@u22:~$ bash dir.sh
directory /home/linuxhint/test does not exist!

Conclusion

This is all about the use of bash scripts to check if files and directories exist. Also, we have used the direct code in the Bash console utilizing test operations and logical operators.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.