Bash Scripting: Check if file exists

When writing a Bash script, it is common that you’ll run into the need to check for the existence of a file. Based on the result, your Bash script can proceed with the appropriate action.

This functionality can be written into a Bash script or used directly from the command line, without writing a script for it. In this tutorial, you will see how to check if a file exists in Bash on Linux systems.

In this tutorial you will learn:

  • How to check if a file exists in Bash script
  • How to check if a file exists from Bash script
Bash Scripting: Check if file exists
Bash Scripting: Check if file exists
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux system
Software Bash shell
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Check if file exists in Bash script




There are multiple ways to check if a file exists, see the methods below:

  1. The first method is by using single brackets [ ] and the -f operator in your if statement, like in the below script:
    FILE=/tmp/error.log
    if [ -f "$FILE" ];
    then
        echo "$FILE file exists."
    else
    	echo "$FILE file does not exist."
    fi
    
    DID YOU KNOW?
    If $FILE happens to be a directory, the script will still say that the file does not exist. If you want to check for the existence of a path as either a file OR a directory, use the -e operator instead of -f.
  2. The next method is a little more succinct, and easier to use on the command line.
    FILE=/tmp/error.log
    [ -f "$FILE" ] && echo "$FILE file exists."
    

    A command line one-liner would look like this:

    $ FILE=/tmp/error.log; [ -f "$FILE" ] && echo "$FILE file exists."
    OR
    $ [ -f /tmp/error.log ] && echo "the file exists."
    
  3. Note that you can also use double brackets [[ ]] in either of the previous examples.
    FILE=/tmp/error.log
    if [[ -f "$FILE" ]];
    then
        echo "$FILE file exists."
    else
    	echo "$FILE file does not exist."
    fi
    
  4. We can also check to see if a file does not exist, by using the ! operator – which is used to negate expressions in Bash.
    FILE=/tmp/error.log
    if [ ! -f "$FILE" ];
    then
    	echo "$FILE file does not exist."
    else
    	echo "$FILE file exists."
    fi
    
  5. What if we want to check whether or not multiple files exist? In that case, it is useful to string together multiple if conditions with && operators or -a as seen below. Note that using the && operator will also require you to use double brackets [[ ]].


    if [[ -f "$FILE1" && -f "$FILE2" ]]; then
        echo "Both $FILE1 and $FILE2 files exist!"
    fi
    

    OR

    if [ -f "$FILE1" -a -f "$FILE2" ]; then
        echo "Both $FILE1 and $FILE2 files exist!"
    fi
    
NOTE
Be sure to put your file in double quotes " " if it contains spaces. Alternatively, wrap the file’s variable in quotes, such as "$FILE" in our examples. This will prevent you from encountering some error. However, you don’t need to wrap the variable in quotes in the echo lines.

Closing Thoughts

In this tutorial, we saw how to check if a file exists from a Bash script or from the command line in Linux. This is a very useful function written into tons of Bash scripts, as many can only proceed if certain files are already known to exist. This takes the guess work out of the equation and will tell your script exactly how to proceed, as checking for the existence of a file only takes the Bash shell a fraction of a second.



Comments and Discussions
Linux Forum