[Bash] Check for a regular file with -f

Created on November 30, 2026

You can use [ -f "$file" ] in a Bash if statement to check whether a path refers to a regular file. This example checks for scores.txt in the current directory.

file="scores.txt"
if [ -f "$file" ]; then
  printf '%s\n' 'The scores file exists'
fi

If the path is a regular file or a symbolic link to one, the command prints:

The scores file exists
Bash