Table of contents
- use a variable set by a script
- Check if File does Not Exist
- Check if Multiple Files Exist
- File test operators
- True if the FILE exists and is a special block file.
- True if the FILE exists and is a special character file.
- True if the FILE exists and is a directory.
- True if the FILE exists and is a file, regardless of type (node, directory, socket, etc.).
- True if the FILE exists and is a regular file (not a directory or device).
- True if the FILE exists and has the same group as the user running the command.
- True if the FILE exists and is a symbolic link.
- True if the FILE exists and has set-group-id (sgid) flag set.
- True if the FILE exists and has a sticky bit flag set.
- True if the FILE exists and is a symbolic link.
- True if the FILE exists and is owned by the user running the command.
- True if the FILE exists and is a pipe.
- True if the FILE exists and is readable.
- True if the FILE exists and is a socket.
- True if the FILE exists and has nonzero size.
- True if the FILE exists, and
set-user-id (suid)
flag is set. - True if the FILE exists and is writable.
- True if the FILE exists and is executable.
- File test operators
- 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
True if the FILE exists and is a symbolic link.
-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
True if the FILE exists and is a symbolic link.
-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