Skip to content

Instantly share code, notes, and snippets.

@woblerr
Created March 17, 2020 08:01
Show Gist options
  • Save woblerr/72f33ce34c81a1709245b77705f6afbd to your computer and use it in GitHub Desktop.
Save woblerr/72f33ce34c81a1709245b77705f6afbd to your computer and use it in GitHub Desktop.
Shell file test operators

Shell file test operators

Usage

FILE=/etc/passwd

if test -f "$FILE"; then
    echo "$FILE exist"
fi

if [ -f "$FILE" ]; then
    echo "$FILE exist"
fi

if [[ -f $FILE ]]; then
    echo "$FILE exist"
fi

test -f "$FILE" && echo "$FILE exist"

[ -f "$FILE" ] && echo "$FILE exist"

[[ -f $FILE ]] && echo "$FILE exist"
Operator Description
-a FILE True if FILE exist
-b FILE True if FILE exists and is a block special file
-c FILE True if FILE exists and is a special character file
-d FILE True if FILE exists and is a directory
-e FILE True if FILE exists and is a file, regardless of type (node, directory, socket, etc.)
-f FILE True if FILE exists and is a regular file (not a directory or device)
-G FILE True if FILE exists and has the same group as the user running the command
-g FILE True if FILE exists and has set-group-id (sgid) bit set
-h FILE True if FILE exists and is a symbolic link
-k FILE True if FILE exists and has a sticky bit bit set
-L FILE True if FILE exists and is a symbolic link
-N FILE True if FILE exists and has been modified since it was last read
-O FILE True if FILE exists and is owned by the user running the command
-p FILE True if FILE exists and is a pipe (FIFO)
-r FILE True if FILE exists and is readable
-S FILE True if FILE exists and is socket
-s FILE True if FILE exists and has nonzero size
-u FILE True if FILE exists and set-user-id (suid) bit is set
-w FILE True if FILE exists and is writable
-x FILE True if FILE exists and is executable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment