How to Check if a File or Directory Exists in Bash?

Photo of author

By admin

If you are working with the bash system on your Linux or Unix computer, you might need to check if a file or directory exists in the bash. Most of the time, when you are working on the Shell scripts, you need to find out a specific file and directory before writing any more inputs in the scripts. But the easiest way to figure out the files or directories is by using some simple commands in the Bash. The commands will tell you if the file exists and what type of file it is that you are looking for. The commands contain these syntaxes:

test EXPRESSION

[ EXPRESSION ]

[[ EXPRESSION ]]

In this article, we are going to show you some simple ways to perform “Bash tests” using commands. For the starter, if you want to make a portable script, you can use the [ command that is available in the POSIX shells. It is an old file test method in the Linux system. But these days, we are using the [[ command to run the same type of bash test, which is a modern method that supports Bash, Zsh, and Ksh shell by default. So, check out the following sections to learn more about these commands.

How to Check if a File Exists in Bash?

-e and -f are the most commonly used file operators that will help you find if a file exists despite its type. With the -e command, you can find a file and with -f, you can find if the file is a regular file or a device or a new directory. Most of the time we use the test command to check if a particular file exists or not. We can use the test command with the if statement. In this example, we are checking if the /etc/resolv.conf file exists in the Bash. And the following commands will help us do that:

FILE=/etc/resolv.conf

if test -f “$FILE”; then

echo “$FILE exists.”

fi

FILE=/etc/resolv.conf

if [ -f “$FILE” ]; then

echo “$FILE exists.”

fi

FILE=/etc/resolv.conf

if [[ -f “$FILE” ]]; then

echo “$FILE exists.”

fi

You can use the if/then structure in the command to know if a file exists in the bash and it will help you perform a new action. For example, you were using the previous commands to install a new file. If you want to update the file, use this command to check if the update is already available or not:

FILE=/etc/resolv.conf

if [ -f “$FILE” ]; then

echo “$FILE exists.”

else

echo “$FILE does not exist.”

fi

Users can use the test command without the if statement. The command that comes after the && operator needs to be performed only when its exit status is true according to the test command:

test -f /etc/resolv.conf && echo “$FILE exists.”

[ -f /etc/resolv.conf ] && echo “$FILE exists.”

Copy

[[ -f /etc/resolv.conf ]] && echo “$FILE exists.”

We use the && as an AND list operator with this syntax: foo && bar. You can run a set of commands after the && operator and for that, you will have to use, or && and those commands will be closed into a third bracket:

[ -f /etc/resolv.conf ] && { echo “$FILE exist.”; cp “$FILE” /tmp/; }

In the command above, you can see && after a statement. If the status of the exists command is false according to the test, the && will have || operators opposite to them like this one in the example below:

[ -f /etc/resolv.conf ] && echo “$FILE exist.” || echo “$FILE does not exist.”

C

That’s how to check a file. But there are times when we need to check directories or multiple files in the Bash. Let’s see what commands we use for that.

Check if a Directory Exists

You can check if a particular file is in a particular directory or not by using the -d command. Here we are checking if the /etc/docker directory exists and for that, we have used the following command:

FILE=/etc/docker

if [ -d “$FILE” ]; then

echo “$FILE is a directory.”

fi

Copy

[ -d /etc/docker ] && echo “$FILE is a directory.”

You can use [[ instead of [. And -d is the short form for checking directories. But you can write the bash script mentioned below to check the bash directory in another way:

#!/bin/bash

if [[ -d /etc ]]

then

echo “/etc exists on your filesystem.”

fi

If the directory exists, the output will be:

$ /etc exists on your filesystem

That’s how you check a directory in the Bash system. But if you want to check multiple files, move on to the next section.

How to Check Multiple Files in Bash?

You could use complex if/else structures to seek files in the Bash. But using the -a and && with [[ will help you with checking multiple files more easily. This is how we have done it:

if [ -f /etc/resolv.conf -a -f /etc/hosts ]; then

echo “Both files exist.”

fi

Copy

if [[ -f /etc/resolv.conf && -f /etc/hosts ]]; then

echo “Both files exist.”

fi

You don’t have to use the if statement and the command will look like this:

[ -f /etc/resolv.conf -a -f /etc/hosts ] && echo “Both files exist.”

Copy

[[ -f /etc/resolv.conf && -f /etc/hosts ]] && echo “Both files exist.”

However, that’s how you check if files and directories exist in the bash. What if you want to know if a particular directory does not exist?

How to Check if a File Does Not Exist?

You will have to use the ! mark to check if a file does not exist in a directory:

FILE=/etc/docker

if [ ! -f “$FILE” ]; then

echo “$FILE does not exist.”

fi

If you want to use the short form, use this:

[ ! -f /etc/docker ] && echo “$FILE does not exist.”

However, you cannot keep checking every day if a particular file or directory exists in the bash. You can create a script instead that will automate the process. If the script does not find a file, it will notify you with a message. Here, we are going to create different file names and use them with the command to check if the test result returns them or not. Use the chmod and make an executable bash script:

$ mkdir -p ~/bin

$ cd ~/bin && touch check_file && chmod u+x check_file && vi check_file

The script will look like this that can check if the files exist in bash in a dynamic manner:

#!/bin/bash

# Using argument expansion to capture all files provided as arguments.

for FILE in ${@}

do

if [[ ! -f $FILE ]]

then

echo “The file ${FILE} does not exist!”

fi

done

Save the previous script and move the bin folder to your path variable so that it can be accessed from wherever:

$ export PATH=”~/bin:$PATH”

$ printenv PATH

~/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Use this command to check if the file exists or not with the new script:

check_file /etc/passwd /etc/pass /etc/file

This new script will list out the files if they don’t exist. Replace the name of the file with etc mentioned in the above command.

File Operators

The word FILE means both files and directories. When we are using test commands, they contain several file operators that help you check for a specific type of file in Bash. Here are those file operators:

  • -L “FILE” : FILE exists and is a symbolic link (same as -h)
  • -h “FILE” : FILE exists and is a symbolic link (same as -L)
  • -d “FILE” : FILE exists and is a directory
  • -w “FILE” : FILE exists and write permission is granted
  • -x “FILE” : FILE exists and execute (or search) permission is granted
  • -r “FILE” : FILE exists and read permission is granted
  • -s “FILE” : FILE exists and has a size greater than zero

These operators will help you check the type of files and directories and compare their values.

Conclusion

We use the file operators to test different properties of a particular file on your Linux distro. A file operator will contain a filename that exists on bash and the operator uses the test command to check if the file exists. The size of these variables is 100 bytes and they have the read, write, and execute permissions for the files and directories in your bash. That’s why the commands we have provided in this article will help you check and figure out whether a particular file exists or not. If you are having any difficulty with this, feel free to ask us for help in the comment box below. Or you can also check out our other relevant articles for assistance.

Leave a Comment