Table of contents
  1. use a variable set by a script
  2. Check if File does Not Exist
  3. Check if Multiple Files Exist
    1. File test operators
      1. True if the FILE exists and is a special block file.
      2. True if the FILE exists and is a special character file.
      3. True if the FILE exists and is a directory.
      4. True if the FILE exists and is a file, regardless of type (node, directory, socket, etc.).
      5. True if the FILE exists and is a regular file (not a directory or device).
      6. True if the FILE exists and has the same group as the user running the command.
      7. True if the FILE exists and is a symbolic link.
      8. True if the FILE exists and has set-group-id (sgid) flag set.
      9. True if the FILE exists and has a sticky bit flag set.
      10. True if the FILE exists and is a symbolic link.
      11. True if the FILE exists and is owned by the user running the command.
      12. True if the FILE exists and is a pipe.
      13. True if the FILE exists and is readable.
      14. True if the FILE exists and is a socket.
      15. True if the FILE exists and has nonzero size.
      16. True if the FILE exists, and set-user-id (suid) flag is set.
      17. True if the FILE exists and is writable.
      18. True if the FILE exists and is executable.
  4. Resources




use a variable set by a script

act=$(gh auth status -t >>(tee -a) 2>&1 | sed -n 's/.*Token: //p');
if [ "$act" == *"$GH_TOKEN"* ](%22$act%22%20==%20*%22$GH_TOKEN%22*.md#)
then do things
fi

Check if File does Not Exist

Similar to many other languages, the test expression can be negated using the ! (exclamation mark) logical not operator:

FILE=/etc/docker
if [ ! -f "$FILE" ]; then
    echo "$FILE does not exist."
fi

Different syntax:

[ ! -f /etc/docker ] && echo "$FILE does not exist."

Check if Multiple Files Exist

Instead of using complicated nested if/else constructs you can use -a (or && with [[) to test if multiple files exist:

if [ -f /etc/resolv.conf -a -f /etc/hosts ]; then
    echo "Both files exist."
fi

differnt syntax

if [hosts ](-f%20/etc/resolv.conf%20&&%20-f%20/etc/hosts.md#); then
    echo "Both files exist."
fi

File test operators

The test command includes the following FILE operators that allow you to test for particular types of files:

True if the FILE exists and is a special block file.

 -b FILE

True if the FILE exists and is a special character file.

 -c FILE

True if the FILE exists and is a directory.

 -d FILE

True if the FILE exists and is a file, regardless of type (node, directory, socket, etc.).

 -e FILE

True if the FILE exists and is a regular file (not a directory or device).

 -f FILE

True if the FILE exists and has the same group as the user running the command.

 -G FILE
 -h FILE

True if the FILE exists and has set-group-id (sgid) flag set.

 -g FILE

True if the FILE exists and has a sticky bit flag set.

 -k FILE
 -L FILE

True if the FILE exists and is owned by the user running the command.

 -O FILE

True if the FILE exists and is a pipe.

 -p FILE

True if the FILE exists and is readable.

 -r FILE

True if the FILE exists and is a socket.

 -S FILE

True if the FILE exists and has nonzero size.

 -s FILE

True if the FILE exists, and set-user-id (suid) flag is set.

 -u FILE

True if the FILE exists and is writable.

 -w FILE

True if the FILE exists and is executable.

-x FILE

Resources