Operations 5 min read

How to Check File Existence in Bash Using test, [ ], [[ ]], and stat Commands

This tutorial explains several Bash techniques—including the test command, single‑bracket and double‑bracket syntaxes, and the stat utility—to reliably determine whether a file exists on a Linux system, complete with example code snippets and a reference table of test flags.

DevOps Operations Practice
DevOps Operations Practice
DevOps Operations Practice
How to Check File Existence in Bash Using test, [ ], [[ ]], and stat Commands

In Linux, checking whether a file exists is a common task for administrators and developers.

The article introduces three primary methods in Bash: the test command, the single‑bracket [ … ] syntax, the double‑bracket [[ … ]] syntax, and the stat command, each with example snippets and explanations of their behavior.

For test , the syntax if test -e "/path/to/file"; then echo "File exists."; else echo "File does not exist."; fi is shown.

The single‑bracket form if [ -e "/path/to/file" ]; then echo "File exists."; else echo "File does not exist."; fi works similarly, while the double‑bracket form if [[ -e "/path/to/file" ]]; then echo "File exists."; else echo "File does not exist."; fi offers extended features such as pattern matching and logical operators.

The stat command can also be used: if stat "/path/to/file" >/dev/null 2>&1; then echo "File exists."; else echo "File does not exist."; fi , which additionally provides detailed file metadata.

An appendix lists common test flags (e.g., -b , -c , -d , -e , -f , etc.) and their meanings.

Linuxbashshell scriptingfile existencestat commandtest command
DevOps Operations Practice
Written by

DevOps Operations Practice

We share professional insights on cloud-native, DevOps & operations, Kubernetes, observability & monitoring, and Linux systems.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.