- Bash Check if File Exists – Tutorial and Commands to Use
- Basic Syntax of Test Command
- Test If File Exists using Command-line
- Test If File Exists in Bash
- Test If Directory Exists
- File Testing Operators in Bash
- Conclusion
- Recent Posts
- How can I check a file exists and execute a command if not?
- 7 Answers 7
- 3 ways to check if file exists in bash
- Check if a File Exists Using the test Command in bash
- Check if a File Exists Using if statement -e option in bash
- Check if a File Exists Using -f option in bash if statement
- File test operators in bash
Bash Check if File Exists – Tutorial and Commands to Use
While creating a bash script, you might encounter a situation where you need to find whether a file exists or not to perform some action with it. You can use the test command to check if file exists and what type is it.
In this tutorial, we will show you how to check if file exists in the Linux-based operating systems.
Basic Syntax of Test Command
The basic syntax of the test command to check if file exists as shown below:
- -e
This option will check whether a file exists regardless of the type. - -f
This option will check whether a file exists only if the FILE is a regular file.
You should see the man page of the test command as shown below:
You should see the following screen:
Test If File Exists using Command-line
In this section, we will show you how to check if file exists using the command line:
Run the following command to check whether a file named /etc/fstab exists (True):
Next, run the following command to set the exit code to 0 or 1, whenever the test succeeded or not.
The output is 0 that means the file /etc/fstab is exists.
You can also use the following command to check if file exists:
Next, run the following command to test if the file /etc/fabab exists (False).
The output is 1 that means the file /etc/fabab is not exists.
You can also test if a file named /etc/rc.local exists or not using the bash conditional execution as shown below:
test -f /etc/rc.local && echo «Exists» || echo «Not Exists»
[ -f /etc/rc.local ] && echo «Exists» || echo «Not Exists»
If the file named /etc/rc.local exists, you should see the following output:
You can also check whether multiple files exists or not using a single command:
[ -f /etc/passwd -a -f /etc/crontab ] && echo «both files exist»
test -f /etc/passwd -a -f /etc/crontab && echo «both files exist»
You should see the following output:
Test If File Exists in Bash
In this section, we will create a bash script and check whether a file exists or not, by printing a message.
Lets, create a file named test.sh and check whether a file /etc/rc.local exists or not:
#!/bin/bash
FILE=/etc/rc.local
if [ -f $FILE ]; then
echo «The file ‘$FILE’ exists.»
else
echo «The file ‘$FILE’ is not exists.»
fi
Save and close the file then run the script with the following command:
You should see the following output:
The file ‘/etc/rc.local’ exists.
Now, let’s create a file test.sh and test whether the file /etc/rc.localad exists of not:
#!/bin/bash
FILE=/etc/rc.localad
if [ -f $FILE ]; then
echo «The file ‘$FILE’ exists.»
else
echo «The file ‘$FILE’ is not exists.»
fi
Save and close the file then run the script with the following command:
You should see the following output:
The file ‘/etc/rc.localad’ is not exists.
You can also find whether a file exists or not and perform some action on it in bash script.
For example, find a file named /etc/hosts and copy it to /opt directory if the file is exists.
To do so, create a file named test.sh:
FILE=/etc/hosts
if [ -d «$FILE» ]; then
cp $FILE /opt
fi
Save and close the file then run the script using the following command:
Now, test the /opt directory with the following command:
You should see the hosts file in the following output:
You can also use the expression ! to check if the file does not exist.
For example, run the following command to check if the file /etc/hosting does not exist:
[ ! -f /etc/hosting ] && echo «File does not exist»
test ! -f /etc/hosting && echo «File does not exist»
You should see the following output:
Test If Directory Exists
You can also find out whether a directory exists or not using the -d option.
For example, check if the directory /etc/apache2 is exists or not.
[ -d /etc/apache2 ] && echo «Directory is exists»
test -d /etc/apache2 && echo «Directory is exists»
You should see the following output:
You can also create a bash script with conditional expressions to check if a directory exists or not.
Create a file named test.sh as shown below:
#!/bin/bash
DIRECTORY=»$1″
if [ -d «$DIRECTORY» ];
then
echo «Directory $DIRECTORY exists.»
else
echo «Directory $DIRECTORY does not exists»
fi
Save and close the file when you are finished. Then, give proper permission with the following command:
Next, test if the directory /etc/nginx is available or not with the following command:
You should see the following output:
Directory /etc/nginx/ exists.
Next, test if the directory /etc/linux is available or not with the following command:
You should see the following output:
Directory /etc/linux does not exists
File Testing Operators in Bash
The following file testing operators will help you to check whether a “file” exists and has specific types:
- -b
FILE exists and is block special - -c
FILE exists and is character special - -d
FILE exists and is a directory - -e
FILE exists - -f
FILE exists and is a regular file - -g
FILE exists and is set-group-ID - -G
FILE exists and is owned by the effective group ID - -h
FILE exists and is a symbolic link (same as -L) - -k
FILE exists and has its sticky bit set - -L
FILE exists and is a symbolic link (same as -h) - -O
FILE exists and is owned by the effective user ID - -p
FILE exists and is a named pipe - -r
FILE exists and read permission is granted - -s
FILE exists and has a size greater than zero - -S
FILE exists and is a socket - -u
FILE exists and its set-user-ID bit is set - -w
FILE exists and write permission is granted - -x
FILE exists and execute (or search) permission is granted
Conclusion
In the above tutorial, we learned how to check if file or directory is available in Bash. We hope you find this bash tutorial useful and Feel free to post your questions below if you have any questions or comments!
Recent Posts
- Forcepoint Next-Gen Firewall Review & Alternatives
- 7 Best JMX Monitoring Tools
- Best PostgreSQL Backup Tools
- Best CSPM Tools
- Best Cloud Workload Security Platforms
- Best Automated Browser Testing Tools
- Event Log Forwarding Guide
- Best LogMeIn Alternatives
- Citrix ShareFile Alternatives
- SQL Server Security Basics
- Cloud Security Posture Management Guide
- Cloud Workload Security Guide
- The Best JBoss Monitoring Tools
- ITL Guide And Tools
- 10 Best Enterprise Password Management Solutions
How can I check a file exists and execute a command if not?
I have a daemon I have written using Python. When it is running, it has a PID file located at /tmp/filename.pid . If the daemon isn’t running then PID file doesn’t exist. On Linux, how can I check to ensure that the PID file exists and if not, execute a command to restart it? The command would be
The «which has to be executed from a specific directory» part of your description sounds like a recipe for trouble. Beware — rethink if at all possible.
/tmp is a bad location to put PID files, since some distributions have cleaner processes that delete files from /tmp, and users may delete files from there to make space.
7 Answers 7
[ -f /tmp/filename.pid ] || python daemon.py restart
-f checks if the given path exists and is a regular file (just -e checks if the path exists)
the [] perform the test and returns 0 on success, 1 otherwise
the || is a C-like or , so if the command on the left fails, execute the command on the right.
So the final statement says, if /tmp/filename.pid does NOT exist then start the daemon.
@Barakuda — A non-regular file could be a directory, named pipe, network socket, character device, symbolic link.
I think && is if the condition is met, while || is if it’s not met. [ -f my_file ] || echo ‘absent’ && echo ‘exists’
test -f filename && daemon.py restart || echo "File doesn't exists"
if only the file needs to be checked for existence : test -f filename && echo «exists» || echo «does not exist»
If it is bash scripting you are wondering about, something like this would work:
if [ ! -f "$FILENAME" ]; then python daemon.py restart fi
A better option may be to look into lockfile
The other answers are fine for detecting the existence of the file. However for a complete solution you probably should check that the PID in the pidfile is still running, and that it’s your program.
Another approach to solving the problem is a script that ensures that your daemon «stays» alive.
Something like this (note: signal handling should be added for proper startup/shutdown):
$PIDFILE = "/path/to/pidfile" if [ -f "$PIDFILE" ]; then echo "Pid file exists!" exit 1 fi while true; do # Write it's own pid file python your-server.py ; # force removal of pid in case of unexpected death. rm -f $PIDFILE; # sleep for 2 seconds sleep 2; done
In this way, the server will stay alive even if it dies unexpectedly.
3 ways to check if file exists in bash
In this blog post, we will discuss 3 different ways to check if a file exists in Bash. This is an important skill to have when you are working with files in a Linux environment. Each of these methods has its own benefits and drawbacks, so it is important to understand them all before deciding which one to use. Let’s get started!
The following Linux commands can be used to check if a file exists in bash.
- test -f /path/to/file && echo “FILE exists.” || echo “File does not exist”
- [ -e /path/to/file ] && echo “FILE exists.” || echo “File does not exist”
- [ -f /path/to/file ] && echo “FILE exists.” || echo “File does not exist”
Check if a File Exists Using the test Command in bash
The first method we will discuss is using the test command. This is a built-in command in Bash that can be used to test various things. In this case, we are interested in using it to check if a file exists. The syntax for this command is as follows:
If the file exists, this command will return a 0 exit code. If the file does not exist, it will return a non-zero exit code. So, we can use this command to check if a file exists like so:
if test -e /path/to/file; then
echo “File exists”
else
echo “File does not exist”
fi
We can do this in one command like this.
test -e /path/to/file && echo “FILE exists.” || echo “File does not exist”
Check if a File Exists Using if statement -e option in bash
The best Linux command to check if a file Exists in bash is using the if statement -e option. The -e option is a built-in operator in Bash to check file exists. If the file exists, this command will return a 0 exit code. If the file does not exist, it will return a non-zero exit code.
The syntax for this operator is as follows:
if [ -e /path/to/file ] ; then
echo “File exists”
else
echo “File does not exist”
fi
We can do this in one command.
[ -e /path/to/file ] && echo “FILE exists.” || echo “File does not exist”
Check if a File Exists Using -f option in bash if statement
The third method we will discuss is using the -f option in if statement. The -e option checks if the file path exists, while the -f option checks if the file path exists and if it is a regular file. The syntax for these operators are as follows:
if [-f /path/to/file ] ; then
echo “File exists”
else
echo “File does not exist”
fi
we can do this in one command line.
[ -f /path/to/file ] && echo “FILE exists.” || echo “File does not exist”
File test operators in bash
The test command includes the following FILE operators that allow you to test for particular types of files:
- -d FILE FILE exists and is a directory.
- -e FILE FILE exists.
- -r FILE FILE exists and the read permission is granted.
- -s FILE FILE exists and it’s size is greater than zero (ie. it is not empty).
- -w FILE FILE exists and the write permission is granted.
- -x FILE FILE exists and the execute permission is granted.
As you can see, there are many different ways to check if a file exists in Bash. Each of these methods has its own benefits and drawbacks, so it is important to understand them all before deciding which one to use. In general, the “test” command is the simplest and most reliable way to check if a file exists. However, the other methods can be useful in certain situations. Thanks for reading!
David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.
howtouselinux.com is dedicated to providing comprehensive information on using Linux.
We hope you find our site helpful and informative.