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.
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:
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 "(/)".