Table of Contents

Filename Generation

If a word contains an unquoted instance of one of the characters *, |, <, [, or ?, it is regarded as a pattern for filename generation, unless the NO_GLOB option is set. If the EXTENDED_GLOB option is set, the ^, ~ and # characters also denote a pattern; otherwise (except for an initial ~, see Filename Expansion above) they are not treated specially by the shell. The word is replaced with a list of sorted filenames that match the pattern. If no matching pattern is found, the shell gives an error message, unless the NULL_GLOB option is set, in which case the word is deleted; or unless the NO_NOMATCH option is set, in which case the word is left unchanged. In filename generation, the character / must be matched explicitly; also, a . must be matched explicitly at the beginning of a pattern or after a /, unless the GLOB_DOTS option is set. No filename generation pattern matches the files "." or "..". In other instances of pattern matching, the / and . are not treated specially.

*
matches any string, including the null string.
?
matches any character.
[...]
matches any of the enclosed characters.
[^...]
matches any character except the enclosed characters.
[!...]
is the same as the above.
<x-y>
matches any number in the range x to y, inclusive. If x is omitted, the number must be less than or equal to y. If y is omitted, the number must be greater than or equal to x. A pattern of the form <-> or simply <> matches any number.

^x
matches anything except the pattern x.

x|y
matches either x or y.

x#
matches zero or more occurrences of the pattern x.
x##
matches one or more occurrences of the pattern x.

Parentheses may be used for grouping. Note that the | character must be within parentheses, so that the lexical analyzer does not think it is a pipe character. Also note that "/" has a higher precedence than "^"; that is:

ls ^foo/bar

will search directories in "." except "./foo" for a file named bar.

A pathname component of the form (foo/)# matches a path consisting of zero or more directories matching the pattern foo. As a shorthand, **/ is equivalent to (*/)#. Thus:

ls (*/)#bar

or

ls **/bar

does a recursive directory search for files named bar.

If used for filename generation, a pattern may contain an exclusion specifier. Such patterns are of the form pat1~pat2. This pattern will generate all files matching pat1, but which do not match pat2. For example, *.c~lex.c will match all files ending in .c, except the file lex.c. This may appear inside parentheses. Note that "~" has a higher precedence than "|", so that
pat1|pat2~pat3 matches any time that pat1 matches, or if pat2 matches while pat3 does not. Note also that "/" characters are not treated specially in the exclusion specifier so that a "*" will match multiple path segments if they appear in the pattern to the left of the "~".

Patterns used for filename generation may also end in a list of qualifiers enclosed in parentheses. The qualifiers specify which filenames that otherwise match the given pattern will be inserted in the argument list. A qualifier may be any one of the following:

/
directories
.
plain files
@
symbolic links
=
sockets
p
named pipes (FIFOs)
*
executable plain files (0100)
%
device files (character or block special)
%b
block special files
%c
character special files
r
readable files (0400)
w
writable files (0200)
x
executable files (0100)
R
world-readable files (0004)
W
world-writable files (0002)
X
world-executable files (0001)
s
setuid files (04000)
S
setgid files (02000)
ddev
files on the device dev
l[-|+]ct
files having a link count less than ct (-), greater than ct (+), or is equal to ct
U
files owned by the effective user id
G
files owned by the effective group id
uid
files owned by user id id if it is a number, if not, than the character after the u will be used as a separator and the string between it and the next matching separator (`(', `[', `{', and `<' match `)', `]', `}', and `>' respectively, any other character matches itself) will be taken as a user name and the user id of this user will be taken (e.g. u:foo: or u[foo] for user foo)
gid
like uid but with group ids or names
a[-|+]n
files accessed within last n days (-), more than n days ago (+), or n days ago
m[-|+]n
files modified within last n days (-), more than n days ago (+), or n days ago
c[-|+]n
files whose inode changed within last n days (-), more than n days ago (+), or n days ago.
If any of the flags a, m, or c is directly followed by a M, w, h, or m (e.g. mh+5) the check is performed with months (of 30 days), weeks, hours, or minutes instead of days,respectively.
L[+|-]n
files less than n bytes (-), more than n bytes (+), or exactly n bytes in length.
^
negates all qualifiers following it
-
toggles between making the qualifiers work on symbolic links (the default) and the files they point to
M
sets the MARK_DIRS option for the current pattern
T
appends a trailing qualifier mark to the file names, analogous to the LIST_TYPES option, for the current pattern (overrides M)
N
sets the NULL_GLOB option for the current pattern
D
sets the GLOB_DOTS option for the current pattern
More than one of these lists can be combined, separated by commas. The whole list matches if at least one of the sublists matches (they are `or'ed', the qualifiers in the sublists are `and'ed').

If a : appears in a qualifier list, the remainder of the expression in parenthesis is interpreted as a modifier (see the subsection Modifiers of the section HISTORY). Note that each modifier must be introduced by a separate :. Note also that the result after modification does not have to be an existing file. The name of any existing file can be followed by a modifier of the form (:..) even if no filename generation is performed.

Thus:

ls *(-/)

lists all directories and symbolic links that point to directories, and

ls *(%W)

lists all world-writable device files in the current directory, and

ls *(W,X)

lists all files in the current directory that are world-writable or world-executable, and

echo /tmp/foo*(u0^@:t)

outputs the basename of all root-owned files beginning with the string "foo" in /tmp, ignoring symlinks, and

ls *.*~(lex|parse).[ch](^D^l1)

lists all files having a link count of one whose names contain a dot (but not those starting with a dot, since GLOB_DOTS is explicitly switched off) except for lex.c, lex.h, parse.c, and parse.h. A "/" at the end of a pattern is equivalent to "(/)".


Mark D. Borges