7.2. File Test Operators: Advanced Bash-Scripting Guide
7.2. File Test Operators: Advanced Bash-Scripting Guide
-e
file exists
-a
file exists
This is identical in effect to -e. It has been "deprecated," [1] and its use is discouraged.
-f
-s
-d
file is a directory
-b
-c
-p
file is a pipe
https://www.tldp.org/LDP/abs/html/fto.html 1/4
3/6/2019 File test operators
function show_input_type()
{
[ -p /dev/fd/0 ] && echo PIPE || echo STDIN
}
-h
-L
-S
file is a socket
-t
This test option may be used to check whether the stdin [ -t 0 ] or stdout [ -t 1 ] in a given script
is a terminal.
-r
file has read permission (for the user running the test)
-w
file has write permission (for the user running the test)
-x
file has execute permission (for the user running the test)
-g
If a directory has the sgid flag set, then a file created within that directory belongs to the group that
owns the directory, not necessarily to the group of the user who created the file. This may be useful for
a directory shared by a workgroup.
-u
A binary owned by root with set-user-id flag set runs with root privileges, even when an ordinary
user invokes it. [2] This is useful for executables (such as pppd and cdrecord) that need to access
system hardware. Lacking the suid flag, these binaries could not be invoked by a non-root user.
https://www.tldp.org/LDP/abs/html/fto.html 2/4
3/6/2019 File test operators
-k
Commonly known as the sticky bit, the save-text-mode flag is a special type of file permission. If a file
has this flag set, that file will be kept in cache memory, for quicker access. [3] If set on a directory, it
restricts write permission. Setting the sticky bit adds a t to the permissions on the file or directory
listing. This restricts altering or deleting specific files in that directory to the owner of those files.
If a user does not own a directory that has the sticky bit set, but has write permission in that directory,
she can only delete those files that she owns in it. This keeps users from inadvertently overwriting or
deleting each other's files in a publicly accessible directory, such as /tmp. (The owner of the directory
or root can, of course, delete or rename files there.)
-O
-G
-N
f1 -nt f2
f1 -ot f2
f1 -ef f2
"not" -- reverses the sense of the tests above (returns true if condition absent).
#!/bin/bash
# broken-link.sh
# Written by Lee bigelow <[email protected]>
# Used in ABS Guide with permission.
# A pure shell script to find dead symlinks and output them quoted
#+ so they can be fed to xargs and dealt with :)
#+ eg. sh broken-link.sh /somedir /someotherdir|xargs rm
#
# This, however, is a better method:
#
# find "somedir" -type l -print0|\
# xargs -r0 file|\
# grep "broken symbolic"|
# sed -e 's/^\|: *broken symbolic.*$/"/g'
https://www.tldp.org/LDP/abs/html/fto.html 3/4
3/6/2019 File test operators
#
#+ but that wouldn't be pure Bash, now would it.
# Caution: beware the /proc file system and any circular links!
################################################################
linkchk () {
for element in $1/*; do
[ -h "$element" -a ! -e "$element" ] && echo \"$element\"
[ -d "$element" ] && linkchk $element
# Of course, '-h' tests for symbolic link, '-d' for directory.
done
}
# Send each arg that was passed to the script to the linkchk() function
#+ if it is a valid directoy. If not, then print the error message
#+ and usage info.
##################
for directory in $directorys; do
if [ -d $directory ]
then linkchk $directory
else
echo "$directory is not a directory"
echo "Usage: $0 dir1 dir2 ..."
fi
done
exit $?
Example 31-1, Example 11-8, Example 11-3, Example 31-3, and Example A-1 also illustrate uses of the file
test operators.
Notes
[2] Be aware that suid binaries may open security holes. The suid flag has no effect on shell scripts.
[3] On Linux systems, the sticky bit is no longer used for files, only on directories.
https://www.tldp.org/LDP/abs/html/fto.html 4/4