While creating a bash script, it is commonly helpful to test if file exists before attempting to perform some action with it.
This is a job for the test
command, that allows to check if file exists and what type is it.
As only the check is done – the test
command sets the exit code to 0
(TRUE
) or 1
(FALSE
), whenever the test succeeded or not.
Also the test
command has a logical “not” operator which allows to get the TRUE
answer when it needs to test if file does not exist.
Cool Tip: Create a clever bash
script! Make it do more tests! Check easily whether a string exists in a file! Read more →
Bash Shell: Test If File Exists
Run one of the below commands to check whether a file exists:
$ test -f FILENAME
– or –
$ [ -f FILENAME ]
Test if the file /etc/passwd
exist (TRUE
):
$ test -f /etc/passwd $ echo $? 0 $ [ -f /etc/passwd ] $ echo $? 0
Test if the file /etc/bebebe
exist (FALSE
):
$ test -f /etc/bebebe $ echo $? 1 $ [ -f /etc/bebebe ] $ echo $? 1
Test if the file /etc/bebebe
does not exist (TRUE
):
$ test ! -f /etc/bebebe $ echo $? 0 $ [ ! -f /etc/bebebe ] $ echo $? 0
If File Exists, Then …
It goes without saying that we don’t just check whether a file exists without a reason.
We usually test if a file exists to perform some action depending on the result of the test.
Maybe if file doesn’t exist – there is no sens to move forward and it is required to break the script or whatever.
In the examples below, lets print corresponding messages depending on the results of the test
command.
Cool Tip: The CASE
statement is the simplest form of the IF-THEN-ELSE
statement! If you have many ELIF
elements – it is better to use the CASE
! Read more →
Test if the file /etc/passwd
exists and print a message if it is TRUE
:
$ if [ -f "/etc/passwd" ]; then echo "The file exists"; fi The file exists
Test if the file /etc/bebebe
does not exist and print a message if it is TRUE
:
$ if [ ! -f "/etc/bebebe" ]; then echo "The file does not exist"; fi The file does not exist
Test if the file exists and print a corresponding message in the each case:
$ [ -f "/etc/passwd" ] && echo "The file exists" || echo "The file does not exist" The file exists $ [ -f "/etc/bebebe" ] && echo "The file exists" || echo "The file does not exist" The file does not exist
Bash Script: Test If File Exists
Lets create a bash
script, that will check whether a passed as an argument file exists or not, by printing a corresponding message.
Create an empty checkfile.sh
file with the touch checkfile.sh
command.
Make it executable with chmod +x checkfile.sh
.
Open the checkfile.sh
with a text editor and put the following code:
#!/bin/bash FILE=$1 if [ -f $FILE ]; then echo "The file '$FILE' exists." else echo "The file '$FILE' in not found." fi
Cool Tip: A good bash
script prints usage and exits if arguments are not provided! It is very simple to configure! Read more →
Save and execute the script as follows:
$ ./checkfile.sh /etc/bebebe The file '/etc/bebebe' is not found. $ ./script.sh /etc/passwd The file '/etc/passwd' exists.
Test If File Exists And It Is …
In the examples above with the -f
operator we only check whether a file exists and it is a regular file.
Here are some other useful options that can help to check whether a “file” exists and has specific permissions or it is a symbolic link, socket or a directory:
Option | Description |
---|---|
-e |
Test if file exists, regardless of type (directory, socket, etc.) |
-f |
Test if file exists and is a regular file |
-r |
Test if file exists and read permission is granted |
-w |
Test if file exists and write permission is granted |
-x |
Test if file exists and execute permission is granted |
-L |
Test if file exists and is a symbolic link |
-S |
Test if file exists and is a socket |
-d |
Test if directory exists |
Run man test
to see all available operators.
very helpful) Great job!!!!
Спасибо!
Great job!!!!Thanks