J O S E P H J O S E P H

Bash File Test Operators

File Test Operators

-e

Return true if file (or directory) exists

if [[ -e /path/to/file ]]; then echo "File or dir exists"; fi

-f

Return true if file exists and is a regular file (not a directory)

if [[ -f /path/to/file ]]; then echo "File exists"; fi

-d

Return true if file exists and is a directory

if [[ -d /path/to/file ]]; then echo "Directory exists"; fi

-s

Return true if file is not zero size

if [[ -s /path/to/file ]]; then echo "File is not empty"; fi

-N

Return true if file has been modified since it was last read

if [[ -N /path/to/file ]]; then echo "File has been modified"; fi

-nt

Return true if file1 is newer (more recent modification time) than file2

if [[ file1 -nt file2 ]]; then echo "File 1 is newer"; fi

-ot

Return true if file1 is older than file2

if [[ file1 -ot file2 ]]; then echo "File 1 is older"; fi