Up Next Previous

File inquiry operators

Some of these operators perform true/false tests on files and related objects. They are of the form -op file, where op is one of

r
Read access

w
Write access

x
Execute access

X
Executable in the path or shell builtin, e.g. `-X ls' and `-X ls-F' are generally true, but `-X /bin/ls' is not (+)

e
Existence

o
Ownership

z
Zero size

s
Non-zero size (+)

f
Plain file

d
Directory

l
Symbolic link (+) *

b
Block special file (+)

c
Character special file (+)

p
Named pipe (fifo) (+) *

S
Socket special file (+) *

u
Set-user-ID bit is set (+)

g
Set-group-ID bit is set (+)

k
Sticky bit is set (+)

t
file (which must be a digit) is an open file descriptor for a terminal device (+)

R
Has been migrated (convex only) (+)

L
Applies subsequent operators in a multiple-operator test to a symbolic link rather than to the file to which the link points (+) *

file is command and filename expanded and then tested to see if it has the specified relationship to the real user. If file does not exist or is inaccessible or, for the operators indicated by `*', if the specified file type does not exist on the current system, then all enquiries return false, i.e. `0'.

These operators may be combined for conciseness: `-xy file' is equivalent to `-x file && -y file'. (+) For example, `-fx' is true (returns `1') for plain executable files, but not for directories.

L may be used in a multiple-operator test to apply subsequent operators to a symbolic link rather than to the file to which the link points. For example, `-lLo' is true for links owned by the invoking user. Lr, Lw and Lx are always true for links and false for non-links. L has a different meaning when it is the last operator in a multiple-operator test; see below.

It is possible but not useful, and sometimes misleading, to combine operators which expect file to be a file with operators which do not, (e.g. X and t). Following L with a non-file operator can lead to particularly strange results.

Other operators return other information, i.e. not just `0' or `1'. (+) They have the same format as before; op may be one of

A
Last file access time, as the number of seconds since the epoch

A:
Like A, but in timestamp format, e.g. `Fri May 14 16:36:10 1993'

M
Last file modification time

M:
Like M, but in timestamp format

C
Last inode modification time

C:
Like C, but in timestamp format

D
Device number

I
Inode number

F
Composite file identifier, in the form device:inode

L
The name of the file pointed to by a symbolic link

N
Number of (hard) links

P
Permissions, in octal, without leading zero

P:
Like P, with leading zero

Pmode
Equivalent to `-P file & mode', e.g. `-P22 file' returns `22' if file is writable by group and other, `20' if by group only, and `0' if by neither

Pmode:
Like Pmode:, with leading zero

U
Numeric userid

U:
Username, or the numeric userid if the username is unknown

G
Numeric groupid

G:
Groupname, or the numeric groupid if the groupname is unknown

Z
Size, in bytes

Only one of these operators may appear in a multiple-operator test, and it must be the last. Note that L has a different meaning at the end of and elsewhere in a multiple-operator test. Because `0' is a valid return value for many of these operators, they do not return `0' when they fail: most return `-1', and F returns `:'.

If the shell is compiled with POSIX defined (see the version shell variable), the result of a file inquiry is based on the permission bits of the file and not on the result of the access(2) system call. For example, if one tests a file with -w whose permissions would ordinarily allow writing but which is on a file system mounted read-only, the test will succeed in a POSIX shell but fail in a non-POSIX shell.

File inquiry operators can also be evaluated with the filetest builtin command (q.v.) (+).

Up Next Previous