Checking Whether Files Exist
Scripts work with files all the time: they read configuration, write logs, copy backups and delete old data. Before a script touches a file, it usually needs an answer to a simple question: is the file there? Bash answers this with file test operators, small flags such as -e and -f that you place inside [ ] or [[ ]]. Each one asks a question about a path and reports the answer through an exit status, so it plugs straight into the if statements from lessons 66 and 67.
Understanding the Concept
A file test does not read a file's contents. It asks the operating system about the path: does something exist there, what kind of thing is it, and how large is it? The answer is an exit status. 0 means the answer is yes and any other value means no, which is the same convention every condition in Bash follows.
A test on a path is a question about the moment it is asked. A file that exists now can be deleted a millisecond later by another process. For simple scripts this does not matter, but it is worth remembering when two programs share the same files.
Syntax
The general form is a unary operator followed by a path:
[ -e path ]
[[ -e path ]]
The operators used for existence checks are:
| Operator | True when |
|---|---|
-e path | anything exists at the path (file, directory, link target, device, and so on) |
-f path | the path exists and is a regular file |
-d path | the path exists and is a directory |
-s path | the path exists and has a size greater than zero |
-L path or -h path | the path is a symbolic link (it may point nowhere) |
The operator ! in front of a test reverses the answer, so [ ! -e path ] is true when nothing exists at the path.
Basic Example
Create a small workspace to experiment with:
mkdir -p reports
printf 'hello\n' > notes.txt
: > empty.txt
The last line uses the : command, which does nothing, together with a redirection that creates an empty file. Now test some paths:
[ -e notes.txt ]; echo "-e notes.txt: $?"
[ -e reports ]; echo "-e reports: $?"
[ -e nothing.txt ]; echo "-e nothing.txt: $?"
Output:
-e notes.txt: 0
-e reports: 0
-e nothing.txt: 1
The -e operator is true for the file and for the directory, because both exist. It is false for nothing.txt, which does not exist.
Understanding the Example
Each line runs a test, then echo prints $?, the exit status of the test that just ran (lesson 43). Status 0 means true. In real scripts you rarely print the status. You place the test directly in an if:
file=notes.txt
if [ -e "$file" ]; then
echo "$file exists"
else
echo "$file is missing"
fi
Output:
notes.txt exists
The variable is quoted inside the test. This is the rule from lesson 55, and it matters even more for file names, which often contain spaces. The section on quoting below shows what happens without it.
Files Versus Directories
-e says that something exists, but it does not say what. When a script must know the type, use a more specific operator:
[ -f notes.txt ]; echo "-f notes.txt: $?"
[ -f reports ]; echo "-f reports: $?"
[ -d reports ]; echo "-d reports: $?"
[ -d notes.txt ]; echo "-d notes.txt: $?"
Output:
-f notes.txt: 0
-f reports: 1
-d reports: 0
-d notes.txt: 1
-f accepts regular files only. A directory exists, so -e is true for it, but -f is false. In the same way, -d is false for an ordinary file. When a script is about to read a file, -f is usually the right check. When it is about to enter a directory or write into one, use -d.
Checking for Empty Files
A file can exist and still contain nothing. The -s operator is true when the file exists and has at least one byte:
[ -s notes.txt ] ; echo "-s notes.txt: $?"
[ -s empty.txt ] ; echo "-s empty.txt: $?"
[ -s nothing.txt ]; echo "-s nothing.txt: $?"
Output:
-s notes.txt: 0
-s empty.txt: 1
-s nothing.txt: 1
The result for a missing file and for an empty file is the same, status 1. If you need to tell them apart, combine two tests, which the section on combining checks shows. The name -s stands for size, and it is easy to misread as "set" or "string".
Symbolic Links
A symbolic link is a small file that stores the path of another file. Most file tests follow the link and look at the target:
ln -s notes.txt link.txt
ln -s missing.txt broken.txt
[ -e link.txt ]; echo "-e link.txt: $?"
[ -L link.txt ]; echo "-L link.txt: $?"
[ -f link.txt ]; echo "-f link.txt: $?"
[ -e broken.txt ]; echo "-e broken.txt: $?"
[ -L broken.txt ]; echo "-L broken.txt: $?"
Output:
-e link.txt: 0
-L link.txt: 0
-f link.txt: 0
-e broken.txt: 1
-L broken.txt: 0
The last two lines show the key difference. broken.txt is a link whose target missing.txt does not exist. The -e test follows the link, finds nothing, and answers no. The -L test looks at the link itself and answers yes. The operator -h is a second name for -L and behaves the same way.
So a script that must detect a dangling link needs -L together with a negated -e:
if [ -L "$path" ] && [ ! -e "$path" ]; then
echo "$path is a broken link"
fi
Quoting Paths Correctly
Paths often contain spaces, and an unquoted variable inside [ ] is split into separate words before the test runs (lesson 53). Create a file with a space in its name and compare the quoted and unquoted forms:
f="my notes.txt"
: > "$f"
[ -e "$f" ]; echo "quoted: $?"
[ -e $f ]; echo "unquoted: $?"
Output:
quoted: 0
bash: [: my: binary operator expected
unquoted: 2
Without quotes, Bash passes two words, my and notes.txt, to [, which does not understand that command line. It reports an error and returns status 2. In a script, the message begins with the script's name and a line number instead of bash:.
Inside [[ ]], the variable is not split, so [[ -e $f ]] works without quotes. Quoting is still a good habit, because the same line copied into a [ ] test would fail.
An empty variable behaves in a way that surprises many people:
p=""
[ -e "$p" ]; echo "-e empty string: $?"
[ -e ]; echo "-e alone: $?"
Output:
-e empty string: 1
-e alone: 0
With quotes, the test receives an empty path, and nothing has an empty name, so the answer is no. Without quotes the variable would vanish and the test would see only the word -e. A test with a single argument is true whenever that argument is not empty, so [ -e ] is true. This is the same rule that made [ -f/etc/hostname ] true in lesson 68, and it is one more reason to quote every variable.
Combining Checks
Use && and || to build richer conditions. Inside [[ ]] you can place them within one pair of brackets:
[[ -e notes.txt && -s notes.txt ]] && echo "exists and non-empty"
Output:
exists and non-empty
With [ ], write two separate tests and join them with the shell's own operators:
if [ -f "$file" ] && [ -s "$file" ]; then
echo "$file is a non-empty regular file"
fi
Lesson 68 explained why -a and -o inside [ ] are best avoided, and lesson 74 covers logical operators in full.
Creating Things Only When Missing
A frequent pattern is "make sure this directory exists". Test first and create if needed:
[ -d out ] || mkdir out
[ -d out ] && echo "out exists now"
Output:
out exists now
The first line runs mkdir only when the directory test fails. For directories, mkdir -p does the job in one step. It creates the directory when it is missing, creates any missing parents, and returns status 0 when the directory already exists:
mkdir -p out
echo "status: $?"
Output:
status: 0
Use mkdir -p when the only goal is to guarantee that a directory exists. Use the explicit test when the script needs to do something different depending on the answer, such as printing a message on first creation.
Comparing Files
A few operators compare two paths instead of asking about one:
| Operator | True when |
|---|---|
file1 -nt file2 | file1 is newer (modified more recently) than file2 |
file1 -ot file2 | file1 is older than file2 |
file1 -ef file2 | both names refer to the same file |
touch -d '2026-01-01' old.txt
touch -d '2026-06-01' new.txt
[ new.txt -nt old.txt ]; echo "new -nt old: $?"
[ old.txt -ot new.txt ]; echo "old -ot new: $?"
[ link.txt -ef notes.txt ]; echo "link -ef notes: $?"
Output:
new -nt old: 0
old -ot new: 0
link -ef notes: 0
The -nt operator is useful for build-style scripts that should rebuild an output only when its source has changed. The -ef operator answers yes for a symbolic link and its target, since both names lead to the same file.
Wildcards Inside File Tests
A pattern such as *.log is expanded by the shell before the test runs (lesson 52). That means [ -e *.log ] does not ask whether any log file exists. It asks a different question depending on how many files match:
[ -e *.log ]; echo "no log files: $?"
touch a.log b.log
[ -e *.log ]; echo "two log files: $?"
Output:
no log files: 1
bash: [: a.log: binary operator expected
two log files: 2
With no match, the pattern stays as the literal text *.log, no file has that name, and the test fails. With two matches, the test receives two names and reports an error. Neither form checks "does any log file exist".
The reliable approach is to loop over the pattern and test each result:
for f in *.log; do
[ -e "$f" ] || continue
echo "found $f"
done
Output:
found a.log
found b.log
The [ -e "$f" ] || continue line handles the case where nothing matched, because then $f holds the literal pattern, -e is false, and the loop skips it. Another option is the nullglob shell option, which makes an unmatched pattern disappear. Combined with an array, it gives a count:
shopt -s nullglob
logs=(*.log)
echo "count: ${#logs[@]}"
shopt -u nullglob
Output:
count: 2
Arrays are covered in lessons 97 to 101, and shopt in lesson 149.
Portability
All the operators in this lesson work with [ ] in Dash, the shell behind sh on Debian and Ubuntu. Running them through sh printed a success message for each of -e, -f, -L, -h and the -a combination. The [[ ]] form is Bash-specific:
sh -c '[[ -e notes.txt ]]'; echo "status: $?"
Output:
sh: 1: [[: not found
status: 127
If a script must run under sh, use [ ] with quoted variables and avoid [[ ]]. Lessons 188 and 189 return to portability in more detail.
Practical Applications
A script that reads a configuration file but falls back to defaults when the file is missing or empty:
#!/usr/bin/env bash
config="$HOME/.myapp.conf"
if [ -s "$config" ]; then
echo "Using settings from $config"
else
echo "No usable config at $config, using defaults"
fi
A script that refuses to continue when a required input is not a regular file:
#!/usr/bin/env bash
input="$1"
if [ ! -f "$input" ]; then
echo "error: '$input' is not a regular file" >&2
exit 1
fi
wc -l "$input"
Run as bash count.sh data.csv, the script prints the line count when data.csv exists. Run with a missing name, it prints the error to standard error (lesson 56) and exits with status 1, which lesson 118 explains further.
Questions such as "can this script write to that directory?" need permission tests such as -r, -w and -x, which the next lesson covers together with file types.