One of the Linux users asked me: I’m creating a bash script in which I want to check if a directory or file is already presented to the user’s machine, and after getting the result, the next command or function should get executed.
Yes, it is possible to check whether a file or directory is presented to the file system by using a conditional statement in a bash script.
So let me show you how you can use this article to find a directory or file that exists in your system by using an if conditional statement, and before that, I’ll introduce you to a test
command that will help you understand logic more clearly.
Table of Contents
Learn about status codes 0 and 1 using test and echo commands
In Bash scripting, the if statement operates a bit differently than in other programming languages, acting on a status or return value.
If the status of the command becomes “0”, which means that the command was executed successfully with a valid result without any errors, but if the status code is other than 0, it means that the command was not executed properly or maybe some problem occurred while executing, which can be properly identified when you check the status code with the program manual.
In Linux, there is a utility that you can use to check whether the file is presented on the system by distinguishing by specifying different types of file options, but the problem is that you will not get the output even if the command is executed successfully.
The only way to know whether the command was executed properly is by executing the echo
command, which will show the status code of the previous command.
If the status code shows you 0, it means the command was executed successfully and the file or directory existed on your system, but if it says 1, it means the command was executed successfully but the file doesn’t exist or there was some problem during execution.
List of options to check if the file exists
Although you know everything is a file in Linux and you can keep the same file name for multiple files, how do you distinguish file types while running a command? To take care of this kind of circumstance, Linux has multiple options which you can use in bash scripting or at a terminal.
Some of the options are listed below:
-d
: Use this when you want to check if a directory exists-e
: Use this when you just want to check if a FILE exists-f
: Use this when you want to check if a REGULAR FILE exists-L
: Use this when you want to check if a symbolic link exists
Now we will see how you can use the above options in test commands as well as in bash scripting.
Let’s see how you can use the test
command with the above options to find the file that does exist on your system. As you know, you will not get the output even if the file is located. Therefore, you can use the echo command to check the status, which will determine the result.
To explain to you better, let me run the below command on my terminal screen:
$ test -e /tmp/
$ echo $?
The output of the above command.
The above status code is 0, which means the file is present on my system.
What would be the behaviour or status code if we mentioned a file that does not exist on my system? Let’s find out the result by invoking the below command on the terminal screen:
$ test -e trendoceans.txt
$ echo $?
The output of the above command.
Hence, it proves the status code 1, which means the file does not exist on my system.
I hope you are able to understand how it works. Let’s see how we can use the following options to determine whether a file is located in the system or not by a bash script.
For Directory:
Sometimes you may need to check whether the specific name of the directory exists or not in the respective system.
As you know, you can set the same file name for the directory name and the regular file, which can cause conflict while executing the script. So to avoid that, you can use the -d option, which will look only for directories.
How do I find if a directory file exists in Bash?
When you want to check whether a directory is present in the system through a terminal, then execute the below code, which will print “Directory found!” if the specified directory is found at the location you mentioned.
As you know, -d is used to check the directory.
Syntax
[ -d <FILENAME> ] && echo "Directory found!"
For example, if you want to check whether the /tmp directory exists or not, just replace the <FILENAME> with an actual file name.
If you want to achieve the same thing using the bash script, then check out the below code snippet:
if [ -d /tmp/ ]; then
echo "Directory found!"
fi-
How do I find if a directory file does not exist in Bash?
Above all, what you have learned is to check if the file exists, but what if the file doesn’t exist? In that case, you should use "!", which will reverse the whole condition, and if the file doesn’t exist, it will print an error.
Syntax
[ ! -d <FILENAME> ] && echo "Directory not found!"
For your sake, let me set any random file name in place of “FILENAME”, and execute the below command.
[ ! -d random ] && echo "Directory not found!"
The behavior of the above command
The same thing we can do with a bash script. You just need to copy and paste the following line into your script file, and just change the file name to the correct one.
if [ ! -d random ]; then
echo "Directory not found!"
fi
Let’s gather all the commands and create a short script that will print “Directory found!” if the directory is present, else it will throw “Directory not found!”
#/bin/bash
if [ -d /tmp ]; then
echo "Directory found!"
else
echo "Directory not found!"
fi
For regular files:
In the previous section, you learned how to tell users that the respective directory does not exist on their system, and you can also check whether regular files like text, images, videos, or anything else exist on the system through a bash script.
How do I tell if a regular file exists in Bash?
First, start with a simple example. If you want to check whether the given file exists on your system in the following location, then use the below command.
[ -f <FILENAME> ] && echo "File found!"
As you know, to check the regular files, we need to use the -f option, which you can say is an acronym for a regular file.
For example, I want to print “File found!” if the sshd_config file exists at the following location.
[ -f /etc/ssh/sshd_config ] && echo "File found!"
The behaviour of the above command.
You can try the same thing in the bash script to check whether the respective file exists on a user’s system. To test it out, you can create a sample bash script file and copy the below code, which will print the output if the file exists.
if [ -f /etc/ssh/sshd_config ]; then
echo "File found!"
fi
How do I tell if a regular file does not exist in Bash
Alternatively, you can use the below command to check whether the specified regular file does not exist by using the terminal itself.
Command Syntax
[ ! -f <FILENAME> ] && echo "File not found!"
Let me try to check whether file trendoceans is available to /etc/ssh and if it is not located then it should print “File not found”.
$ [ ! -f /etc/ssh/trendoceans ] && echo "File not found!"
The output of the above command.
You can use the above condition in the bash script to check whether a regular file does not exist.
if [ ! -f /etch/ssh/trendoceans.txt ]; then
echo "File not found!"
fi
The problem with the above code was that it prints output only when a file does not exist, but I wanted the user to also get informed when the file exists. Alright, let me share with you another code snippet that will print the output as per the given conditions.
#/bin/bash
if [ ! -f /tmp/trendoceans.txt ]; then
echo "File not found!"
else
echo "File found!"
fi
For symbolic link files:
Above you have learned how to check whether directory and files exist on the system by bash script now you will learn how to find the symbolic link using a bash script.
How to check if a symlink or symbolic exists in Bash
To check a symlink or symbolic link, use -L, which will only show the result if the corresponding file is a soft link. If you want to create a new symbolic link, follow this article.
[ -L <FILENAME> ] && echo "Symbolic link found"
Let me create a symbolic link of the /tmp directory to the current path and then execute the below command to check the symbolic link.
$ ln -s /tmp sym-tmp
$ [ -L sym-tmp ] && echo "Symbolic link found"
The output of the above command.
For bash scripting, you can use the below code:
if [ -L sym-tmp ]; then
echo "Symbolic link found!"
fi
How do I tell if a symbolic file does not exist in Bash
Append the negation "!" operator next to -L to find a symbolic file that does not exist in Bash.
$ [ ! -L <FILENAME> ] && echo "Symbolic link found"
You can use the below code to check whether a symbolic file does not exist in the Bash script.
if [ ! -L sym-tmp ]; then
echo "Symbolic link found!"
fi
If you want to create a short script where you can test the feature, then copy and paste the below code:
#/bin/bash
if [ ! -L sym-tmp ]; then
echo "Symbolic link file not found!"
else
echo "Symbolic link file found!"
fi
Wrap up
That’s all for now!
In this article, you have learned how to check whether a regular file, directory, or soft link file exists on the user system through shell scripting.
To learn more, you can subscribe to our newsletter for weekly updates.
A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.