A Shell script might demand that you check whether a file exists before doing a task.
You could always assume that the programmer or user that will run the script will do their due diligence and ensure the file is present. But bash offers the ability to check that a file exists, and leaving it to chance will be the clumsy thing to do.
Also, assuming a file is present isn’t the right way to go if the script is distributed on various operating systems.
Even if your script is successful on most computers that run it, there will eventually be a case where a computer doesn’t satisfy your assumption.
Then, the script will execute unpredictably, mistaking one file for another, which might harm the operating system or do significant damage. Or the script will fail altogether.
There are a few different ways you can determine whether the file exists and find the details of the file. The nice thing about all the methods is that you will be able to quickly write the code and use it with any kind of program.
One of the most prominent ways of doing it is to use a “test” command. In this brief guide, we will see how you can use this method and four other methods to check if a file exists in bash.
The Primary Expressions in Bash
Writing an if statement with a relevant test lets you determine whether a file exists in a matter of seconds. The statement also allows you to determine whether the file in question is readable, executable, or has other properties.
The test syntax statements you might find invaluable in your checking and testing of files include the following:
|Syntax Statement||Returns “True” If the File…|
|-c||Contains special characters|
|-d||Is a directory|
|-f||Exists and is an ordinary file type|
|-b||Is a “block special file”|
|-g||Has the setgid permission (chmod g+)|
|-G||If your group owns the file|
|-h||Is a symbolic link|
|-k||Has the sticky bit enabled (chmod +t)|
|-L||Is a symbolic link|
|-N||If the file was altered since the previous time it was read|
|-O||If you own the file|
|-p||Is a named pipe|
|-r||Can be read|
|-s||Does not exist or if it contains data|
|-S||Is a socket|
|-t||If the file descriptor is accessed from a terminal|
|-u||Has the setuid permission set (chmod u+)|
|-w||If it can be written to|
|-x||If it can be executed|
There are three other symbols you can use. These are the !, &&, and || symbols. The first one represents the NOT operator, the second the AND operator, and the third the OR operator.
Checking If a File Exists in Bash
Before we get into the details of the methods, let’s see how you can make a file for any bash to find. Here’s how you’d create a “sampleFile.sh” script:
Now, open this empty script file by using the nano command like so:
The file will open in the text editor. You can compose the script as you like and save it. With this, we’re ready to get into checking if a file exists using bash.
Method #1: With the Bash Test Command
The test command is quite flexible and can be used to check many things. Checking for a file’s existence is one of the things the command can help accomplish.
Here’s what its syntax looks like:
|test -e /path/to/file|
If the file exists, you will see the 0 exit code appear. But if it doesn’t exist, a non-zero exit code will appear.
if test -e $FILE; then
echo “$FILE exists”
echo “$FILE does not exist”
Method #2: Entering the File’s Name When Composing the Script
One of the other methods of finding a file is to supply its name when writing the script. You can approach this in three ways.
One, you could use the test command again. Two, you could use an if statement with an expression inside closed brackets. And three, you could use an if statement with an expression inside double square brackets.
Let’s look at examples of all three to understand them better.
Open an editor and save the following in a file:
if test -f “$filename”;
echo $”file exists”
echo $”file does not exist”
Execute the file, and you will see the output “./sampleFile.sh.” But if the directory doesn’t have the file, you will see the “file does not exist” message.
You have to do the same thing you did with the previous example. Copy the following script, open an editor, and save it in a file:
if [ -f “$filename” ];
echo $”filename exists”
echo $”filename does not exist”
Go back to the console and run the file. You will see the output “./sampleFile.sh.”
Again, open an editor, put the following code in the file, and save it:
if [[ -f “$filename” ]];
echo $”filename exists”
echo $”filename does not exist”
You will see the same result as the previous two examples.
Method #3: Putting the File’s Name into the Terminal
It’s also possible for you to ask the user for the file’s name they’re looking for in the terminal. Checking the existence of a file is as simple as using “-f.”
Here’s the script you’ll need:
echo “Enter your filename.”
if [ -f “$sampleFile” ]
echo “File exists”
echo “File does not exist”
Save this script in a file and run it. You can then determine the existence of the file.
Bear in mind that running the code above will make a “permission denied” message appear on the console.
But this not a cause for worry. All you need to do is make the file executable. Simply run the line we’ve mentioned below.
|chmod +x fosslinux.sh|
Method #4: With the Bash If Statement -e Option
Using the -e option with the if statement is perhaps the best approach to determine if a file exists in bash. The option is a built-in bash operator designed explicitly for this purpose.
If the file exists, a 0 exit code will appear. Otherwise, a non-zero exit code will appear.
To see this command in action, run the following in your bash shell:
|[ -e sampleFile.txt ] && echo “File exists.” || echo “File does not exist”|
Method #5: Using the -f Flag in The Bash If Statement
The final method of checking whether the file exists in bash we’ll discuss in this post involves using the -f option.
In contrast to the -e option, the -f option checks if the file path exists and whether the file in question is normal.
Here’s what a command involving this option would look like:
|[ -f sampleFile.txt ] && echo “File exists.” || echo “File does not exist”|
Now that you understand the different ways of checking whether a file exists, ensure you test and don’t assume. If you assume a file exists or leave anything else to chance, your program might fail catastrophically, giving you a bad name.
The more you learn about how your program works, the better authority you’ll have on fixing it.