Submitting the form below will ensure a prompt response from us.
When writing Bash scripts, one of the most common tasks is verifying whether a file exists before performing operations such as reading, writing, copying, deleting, or processing data. Checking file existence helps prevent runtime errors and makes shell scripts more reliable and efficient.
Bash provides several built-in test operators that allow developers and system administrators to determine whether a file, directory, symbolic link, or other filesystem object exists. These checks are commonly used in automation scripts, deployment pipelines, backup systems, and server management tasks.
In this guide, you’ll learn multiple ways to Bash Check if File Exists, along with practical examples, best practices, and common mistakes to avoid.
Before accessing a file, it’s important to verify that it is available.
Common use cases include:
A file existence check helps ensure that your script executes safely and avoids unexpected failures.
The most common method for checking whether a regular file exists is the -f operator.
Example
FILE="config.txt"
if [ -f "$FILE" ]; then
echo "File exists."
else
echo "File does not exist."
fi
Output
File exists.
What -f Checks
The -f operator returns true only if:
If you want to check whether any filesystem object exists, use the -e operator.
Example
PATH_NAME="sample.txt"
if [ -e "$PATH_NAME" ]; then
echo "Path exists."
else
echo "Path does not exist."
fi
Output
Path exists.
What -e Checks
The -e operator returns true if the path exists, regardless of whether it is:
To verify whether a directory exists, use the -d operator.
Example
DIRECTORY="/var/log"
if [ -d "$DIRECTORY" ]; then
echo "Directory exists."
else
echo "Directory does not exist."
fi
Output
Directory exists.
This method is commonly used in deployment and backup scripts.
Use the -L operator to determine whether a symbolic link exists.
Example
LINK="shortcut"
if [ -L "$LINK" ]; then
echo "Symbolic link exists."
else
echo "Symbolic link not found."
fi
Output
Symbolic link exists.
Bash also supports double brackets, which provide additional flexibility and improved readability.
Example
FILE="report.txt"
if [[ -f "$FILE" ]]; then
echo "File exists."
fi
Double brackets are often preferred in modern Bash scripts.
Sometimes you need to execute code only when a file is missing.
Example
FILE="backup.zip"
if [ ! -f "$FILE" ]; then
echo "File not found."
fi
Output
File not found.
The exclamation mark (!) negates the condition.
A common use case is creating a file only when it doesn’t already exist.
Example
FILE="output.log"
if [ ! -f "$FILE" ]; then
touch "$FILE"
echo "File created."
fi
Output
File created.
You can verify the existence of multiple files within a loop.
Example
for FILE in app.log config.json data.csv
do
if [ -f "$FILE" ]; then
echo "$FILE exists."
else
echo "$FILE not found."
fi
done
This approach is useful when validating application dependencies.
if [ -f "/etc/app.conf" ]; then
echo "Configuration loaded."
fi
if [ -f "server.log" ]; then
cat server.log
fi
if [ -f "database_backup.sql" ]; then
echo "Backup available."
fi
| Operator | Purpose | Checks |
|---|---|---|
| -e | Path Exists | Returns true if the specified path (file, directory, symbolic link, or other filesystem object) exists. |
| -f | Regular File | Returns true if the path exists and is a regular file. |
| -d | Directory | Returns true if the specified path is an existing directory. |
| -L | Symbolic Link | Returns true if the path is a symbolic link to an existing file. |
| -r | Readable File | Returns true if the file exists and has read permission. |
| -w | Writable File | Returns true if the file exists and has write permission. |
| -x | Executable File | Returns true if the file exists and has execute permission. |
| -s | Non-Empty File | Returns true if the file exists and its size is greater than zero. |
These operators can be combined to create more advanced validation logic.
Always wrap file paths in quotes to handle spaces correctly.
Correct:
if [ -f "$FILE" ]; then
echo "Exists"
fi
If you specifically need a file, prefer -f over -e.
Avoid using file checks when validating directories.
Display meaningful messages or create fallback files when resources are unavailable.
Validate scripts on development, staging, and production systems to ensure consistent behavior.
Incorrect:
if [ -f $FILE ]; then
echo "Exists"
fi
If the filename contains spaces, this may cause unexpected behavior.
Correct:
if [ -f "$FILE" ]; then
echo "Exists"
fi
Incorrect:
if [ -e "$PATH_NAME" ]; then
echo "Found"
fi
This condition may return true for directories and symbolic links.
Instead, use:
if [ -f "$PATH_NAME" ]; then
echo "Regular file found"
fi
when you’re specifically checking for files.
Build Reliable Automation and DevOps Solutions
Our experts develop custom automation scripts, CI/CD pipelines, and cloud-native solutions to streamline business operations.
Checking whether a file exists is a fundamental Bash scripting task that improves script reliability and prevents unnecessary errors. Bash provides several file test operators, such as -f, -e, -d, and -L, allowing developers to easily verify files, directories, and symbolic links.
For most file existence checks, -f is the preferred choice because it ensures the path is a regular file. By combining file tests with conditional statements and following Bash scripting best practices, you can build robust automation scripts, deployment tools, and system administration workflows that handle files safely and efficiently.