×
bash check if file exists tutorial

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:

test -e file
[ -e file ]

Or

test -f file
[ -f file ]

Where:

  • -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:

man test

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):

test -f /etc/fstab

Next, run the following command to set the exit code to 0 or 1, whenever the test succeeded or not.

echo $?

Output:

0

The output is 0 that means the file /etc/fstab is exists.

You can also use the following command to check if file exists:

[ -f /etc/fstab ]
echo $0

Output:

0

Next, run the following command to test if the file /etc/fabab exists (False).

test -f /etc/fabab
echo $?

Output :

1

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"

Or

[ -f /etc/rc.local ] && echo "Exists" || echo "Not Exists"

If the file named /etc/rc.local exists, you should see the following output:

Exists

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"

Or

test -f /etc/passwd -a -f /etc/crontab && echo "both files exist"

You should see the following output:

both files exist

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:

nano test.sh

Add the following lines:

#!/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:

bash test.sh

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:

nano test.sh

Add the following lines:

#!/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:

bash test.sh

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:

nano test.sh

Add the following lines:

FILE=/etc/hosts
if [ -d "$FILE" ]; then
cp $FILE /opt
fi

Save and close the file then run the script using the following command:

bash test.sh

Now, test the /opt directory with the following command:

ls /opt/hosts

You should see the hosts file in the following output:

/opt/hosts

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"

Or

test ! -f /etc/hosting && echo "File does not exist"

You should see the following output:

File does not exist

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"

Or

test -d /etc/apache2 && echo "Directory is exists"

You should see the following output:

Directory is exists

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:

nano test.sh

Add the following lines:

#!/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:

chmod 755 test.sh

Next, test if the directory /etc/nginx is available or not with the following command:

./test.sh /etc/nginx

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:

./test.sh /etc/linux

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!