How to Check If a File or Directory Exists From Bash

Learn how to check If a file or directory exists from bash, we will look at the traditional method of checking by hand and using the test command from within shell scripts.

On this page
  1. Traditional Method
  2. Advanced File Checking In Shell Scripts
  3. Conclusion

Linux Systems and Unix systems are composed of multiple files and directories. So it is likely that at some point you will need to verify if specific files or directories exist. In this blog post, we will look at the traditional method of checking by hand and using the test command from within shell scripts. The shell script section will break down flags, and syntax options, in addition to some examples.

#Traditional Method

If you’re working from a shell the ls command or stat command are the most common commands to check if a file or directory exists. Below are some examples of these commands.

Using ls

Code
ls path

Using stat

Code
stat path

#Advanced File Checking In Shell Scripts

The methods we explored won’t cut it for scripting, below we will explore some better methods suited for scripts. Instead, for scripts we will use part of GNU coreutils called the test command.

The flags used will change what is being tested, below are the flags I think will be most useful:

These are some examples taken from the man page for the test command which you can view by typing man test.

Code
man test

POSIX Compliant

There are two different options for use of the test command that are POSIX compliant. By sticking to the POSIX standard, meaning you can use the test command on any system that uses POSIX shells not just on Linux but Unix-like operating systems as well.

Using the test command directly:

Code
test -f path

In this command, the -f flag stands for regular files. Also, replace the path with the full file path including the file name.

Another method is to put the flag and path inside one line of square brackets as shown in the following command.

Code
[ -f "path" ]

[ ] is treated as the equivalent of the “test” command, it syntax is just shorter.

Double Square Brackets

In certain scenarios where you are using Bash shell, Zsh, Ksh, and other compatible shells you can use the following syntax.

Code
[[ -f path ]]

Using this syntax with a compatible shell will provide better error handling and no need to quote

Shell Script Examples

Here are three examples of scripts (multiple commands strung together) all using an if statement. One script using the test command, one script file with double square brackets, and one script incorporating an Associative Array.

Using the test command:

Code
# Prompt the user to enter the file path
read -p "Enter the file path: " file_path

if test -f "$file_path"; then
    echo "File exists: $file_path"
    # Add your actions here, such as reading the file or processing its contents
else
    echo "File does not exist: $file_path"
    # Add error handling or other actions for when the file does not exist
fi

Bash Script:

Code
#!/bin/bash

# Prompt the user to enter the file path
read -p "Enter the file path: " file_path

if [[ -f $file_path ]]; then
    echo "File exists: $file_path"
    # Add your actions here, such as reading the file or processing its contents
else
    echo "File does not exist: $file_path"
    # Add error handling or other actions for when the file does not exist
fi

This next example will loop through multiple files at once using an array.

Code
#!/bin/bash

# Prompt the user to enter a list of file paths separated by spaces
read -p "Enter a list of file paths separated by spaces: " file_paths

# Split the input into an array using space as the delimiter
IFS=' ' read -r -a file_paths_array <<< "$file_paths"

# Loop through each file path in the array
for file_path in "${file_paths_array[@]}"
do
    if [ -f "$file_path" ]; then
        echo "File exists: $file_path"
        # Add your actions here for when the file exists
    else
        echo "File does not exist: $file_path"
        # Add error handling or other actions for when the file does not exist
    fi
done

Bonus: Would you like to do the exact opposite of the test use an exclamation mark before what you want the inverse of.

#Conclusion

Though traditional methods like ls and stat are perfectly suited for checking if a file or directory exists manually, the same can’t be said when scripting this is where the test command comes into place. With the test command, we explore POSIX-compliant and noncompliant options. From there wrap up with some code examples.

Illustrated avatar of CJ Saathoff
Written by

CJ Saathoff

CJ Saathoff writes for the NameHero blog.

More from CJ →