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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BashShell 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

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.