Bash – Check If File Exists
Bash Script to Check If File Exists – To check if file exists in bash scripting, use the test expression [ -e FILE ] inside a bash if statement. The -e test returns true when the given path exists. The older -a expression is seen in some scripts, but -e is the clearer and preferred form for checking existence.
In this tutorial, we will go through Bash Script examples to check if file exists using both [ -a FILE ] and [ -e FILE ], then cover practical variations such as checking only regular files, checking directories, handling paths stored in variables, and testing whether a file does not exist.
Bash file existence test syntax with -e
The basic syntax is as follows. Always keep spaces around the square brackets, because [ is a command in shell syntax.
if [ -e FILE ]; then
echo "File exists"
else
echo "File does not exist"
fi
Use quotes when the file path comes from a variable or may contain spaces.
file_path="/home/tutorialkart/sample.txt"
if [ -e "$file_path" ]; then
echo "File exists"
fi
Examples to check whether a file exists in Bash
- Example 1 – Using [ -a FILE ] (Depreciated method to check in bash if file exists.)
- Example 2 – Using [ -e FILE ] (Preferred method to check in bash if file exists.)
- Example 3 – Using [ ! -e FILE ] to check if a file does not exist.
- Example 4 – Using [ -f FILE ] to check if the path is a regular file.
- Example 5 – Checking a file path stored in a Bash variable.
We shall use a sample.txt file for this example
arjun@arjun-VPCEH26EN:/home/tutorialkart$ ls -lt
total 125012
-rw-r--r-- 1 root root 12 Oct 5 09:35 sample.txt
Example 1 – Using [ -a FILE ] expression with if statement
For this example, there is a file /home/tutorialkart/sample.txt present and /home/tutorialkart/dummy.txt not present. We shall demonstrate the result with the help of following example.
#!/bin/bash
# Scenario - File exists
if [ -a /home/tutorialkart/sample.txt ];
then
echo "sample.txt - File exists."
else
echo "sample.txt - File does not exist."
fi
# Scenario - File does not exists
if [ -a /home/tutorialkart/dummy.txt ];
then
echo "dummy.txt - File exists."
else
echo "dummy.txt - File does not exist."
fi
When the above bash shell script is run in Terminal
arjun@arjun-VPCEH26EN:~/workspace/bash$ ./bash-script-if-file-exists
sample.txt - File exists.
dummy.txt - File does not exist.
This works in many Bash environments, but prefer -e in new scripts. It communicates the intention more clearly: test whether the path exists.
Example 2 – Using [ -e FILE ] expression with if statement
We shall use the same files as in Example 1, but with a new expression [ -e FILE ]
#!/bin/bash
# Scenario - File exists
if [ -e /home/tutorialkart/sample.txt ];
then
echo "sample.txt - File exists."
else
echo "sample.txt - File does not exist."
fi
# Scenario - File does not exists
if [ -e /home/tutorialkart/dummy.txt ];
then
echo "dummy.txt - File exists."
else
echo "dummy.txt - File does not exist."
fi
When the above bash shell script is run in Terminal
arjun@arjun-VPCEH26EN:~/workspace/bash$ ./bash-script-if-file-exists-2
sample.txt - File exists.
dummy.txt - File does not exist.
The -e operator checks whether the path exists. The path may point to a regular file, directory, symbolic link, socket, FIFO, or another filesystem entry. If you specifically want a regular file, use -f instead of -e.
Example 3 – Check if a file does not exist in Bash
To check whether a file does not exist, place the logical NOT operator ! before the file test expression.
#!/bin/bash
file_path="/home/tutorialkart/dummy.txt"
if [ ! -e "$file_path" ]; then
echo "$file_path does not exist."
else
echo "$file_path exists."
fi
Output:
/home/tutorialkart/dummy.txt does not exist.
This pattern is useful before creating a file, downloading a file, or writing a default configuration only when it is missing.
Example 4 – Check if the path is a regular file with -f
The -e test only tells us that a path exists. If the script must confirm that the path is a regular file and not a directory, use -f.
#!/bin/bash
file_path="/home/tutorialkart/sample.txt"
if [ -f "$file_path" ]; then
echo "The path exists and is a regular file."
else
echo "The path is missing or is not a regular file."
fi
Output:
The path exists and is a regular file.
For scripts that read from or write to normal files, -f is often more precise than -e.
Example 5 – Check file existence from a Bash variable
In real scripts, file paths are often stored in variables. Quote the variable inside the test expression to handle spaces and empty values safely.
#!/bin/bash
backup_file="/home/tutorialkart/my backup.txt"
if [ -e "$backup_file" ]; then
echo "Backup file found: $backup_file"
else
echo "Backup file is missing: $backup_file"
fi
Without quotes around $backup_file, a path such as my backup.txt may be split into separate words and cause an incorrect test or a shell error.
Bash file test operators related to file existence
Bash provides several file test operators. Choose the one that matches the exact condition your script needs.
| Test expression | Meaning | Use when |
|---|---|---|
[ -e FILE ] | Path exists | You only need to know whether the path is present. |
[ -f FILE ] | Path exists and is a regular file | You are expecting a normal file, not a directory. |
[ -d FILE ] | Path exists and is a directory | You need to verify a folder path. |
[ -s FILE ] | File exists and has size greater than zero | You need to confirm the file is not empty. |
[ -r FILE ] | File exists and is readable | Your script needs to read the file. |
[ -w FILE ] | File exists and is writable | Your script needs to write to the file. |
[ -x FILE ] | File exists and is executable | Your script needs to run the file as a command. |
[ -L FILE ] | Path exists and is a symbolic link | You need to detect symlinks. |
Check a directory path instead of a file in Bash
If you are checking for a directory, use -d. This avoids treating a file and a directory as the same kind of path.
#!/bin/bash
dir_path="/home/tutorialkart/logs"
if [ -d "$dir_path" ]; then
echo "Directory exists."
else
echo "Directory does not exist."
fi
Common mistakes when checking if a file exists in Bash
- Missing spaces around brackets: Write
[ -e "$file" ], not[-e "$file"]. - Not quoting variables: Use
"$file"so paths with spaces are handled correctly. - Using -e when -f is required:
-eaccepts directories too. Use-ffor regular files. - Checking and then writing without considering changes: Another process can create or remove the file after your test. For critical scripts, handle the file operation error as well.
- Confusing file existence with file permissions: A file may exist but still not be readable or writable by the current user.
Bash file exists FAQ
What is the best way to check if a file exists in Bash?
Use [ -e "file_path" ] to check whether a path exists. If you specifically need a regular file, use [ -f "file_path" ].
What is the difference between -e and -f in Bash?
-e returns true when the path exists, whether it is a file, directory, symlink, or another filesystem entry. -f returns true only when the path exists and is a regular file.
How do I check if a file does not exist in Bash?
Use [ ! -e "file_path" ]. The ! operator negates the test, so the condition becomes true when the file path is missing.
Should I use single brackets or double brackets for Bash file tests?
Both [ -e "$file" ] and [[ -e "$file" ]] are commonly used in Bash. Single brackets are POSIX-style and work in many shells. Double brackets are Bash syntax and provide safer expression handling in Bash scripts.
Why should file path variables be quoted in Bash tests?
Quoting prevents word splitting and glob expansion. This matters when a file path contains spaces, wildcard characters, or is empty.
QA checklist for this Bash file exists tutorial
- Use
-eas the preferred existence check and explain when-fis more precise. - Keep all Bash file path variables quoted in newly added examples.
- Show the negative check with
[ ! -e FILE ]for missing files. - Distinguish regular files, directories, symlinks, and permission checks where relevant.
- Ensure new command examples use PrismJS-compatible
language-bash,syntax, oroutputclasses.
Conclusion
In this Bash Tutorial – Bash – Check If File Exists, we have learnt to check if a specified file exists using [-e FILE] condition. For regular files, use -f; for directories, use -d; and for missing files, use [ ! -e FILE ]. Choosing the right file test makes Bash scripts easier to read and less error-prone.
TutorialKart.com