LOGBOOK

HELP

Quiz Entry - updated: 2026.07.14

What are the file test operators in Bash?

File tests check existence and properties: -f (regular file), -d (directory), -e (exists, any type), -r/-w/-x (readable/writable/executable).

These are the backbone of defensive scripting — check before you act. -e answers "does this path exist at all?", while -f and -d distinguish a regular file from a directory. The permission tests (-r/-w/-x) check whether the current user may access it, which is more honest than assuming: a file can exist yet be unreadable to you. Guard clauses like [ -f "$1" ] || { echo "no such file" >&2; exit 1; } make scripts fail loudly instead of doing the wrong thing.

Operator Tests if...
-f FILE File exists and is regular file
-d FILE Directory exists
-e FILE File/directory exists (any type)
-r FILE File is readable
-w FILE File is writable
-x FILE File is executable
-s FILE File exists and is not empty
-L FILE File is symbolic link

Examples:

# Check if file exists
if [ -f /etc/passwd ]; then
    echo "passwd file exists"
fi

# Check if directory exists
if [ -d /home/user ]; then
    echo "Home directory exists"
fi

# Check if script is executable
if [ -x ./myscript.sh ]; then
    ./myscript.sh
else
    echo "Script not executable"
fi

Combine tests:

if [ -f "$file" ] && [ -r "$file" ]; then
    cat "$file"
fi

Go deeper:

From Quiz: LIOS / Bash Scripting and Automation | Updated: Jul 14, 2026