1GLOB(7)                    Linux Programmer's Manual                   GLOB(7)


6       glob - globbing pathnames


9       Long  ago,  in UNIX V6, there was a program /etc/glob that would expand
10       wildcard patterns.  Soon afterward this became a shell built-in.
12       These days there is also a library routine glob(3)  that  will  perform
13       this function for a user program.
15       The rules are as follows (POSIX.2, 3.13).
17   Wildcard matching
18       A  string  is  a  wildcard pattern if it contains one of the characters
19       '?', '*' or '['.  Globbing is the operation  that  expands  a  wildcard
20       pattern  into  the list of pathnames matching the pattern.  Matching is
21       defined by:
23       A '?' (not between brackets) matches any single character.
25       A '*' (not between brackets) matches any string,  including  the  empty
26       string.
28       Character classes
30       An  expression  "[...]" where the first character after the leading '['
31       is not an '!' matches a single character, namely any of the  characters
32       enclosed  by  the brackets.  The string enclosed by the brackets cannot
33       be empty; therefore ']' can be allowed between the  brackets,  provided
34       that it is the first character.  (Thus, "[][!]" matches the three char‐
35       acters '[', ']' and '!'.)
37       Ranges
39       There is one special convention: two characters separated by '-' denote
40       a    range.    (Thus,   "[A-Fa-f0-9]"   is   equivalent   to   "[ABCDE‐
41       Fabcdef0123456789]".)  One may include '-' in its  literal  meaning  by
42       making  it  the  first  or last character between the brackets.  (Thus,
43       "[]-]" matches just the two characters ']' and '-', and "[--0]" matches
44       the three characters '-', '.', '0', since '/' cannot be matched.)
46       Complementation
48       An expression "[!...]" matches a single character, namely any character
49       that is not matched by the expression obtained by  removing  the  first
50       '!'  from it.  (Thus, "[!]a-]" matches any single character except ']',
51       'a' and '-'.)
53       One can remove the special meaning of '?', '*'  and  '['  by  preceding
54       them  by a backslash, or, in case this is part of a shell command line,
55       enclosing them in quotes.  Between brackets these characters stand  for
56       themselves.   Thus,  "[[?*\]" matches the four characters '[', '?', '*'
57       and '\'.
59   Pathnames
60       Globbing is applied on each of the components of a pathname separately.
61       A '/' in a pathname cannot be matched by a '?' or '*' wildcard, or by a
62       range like "[.-0]".  A range containing an explicit  '/'  character  is
63       syntactically  incorrect.  (POSIX requires that syntactically incorrect
64       patterns are left unchanged.)
66       If a filename starts with a '.', this character must be matched explic‐
67       itly.   (Thus,  rm * will not remove .profile, and tar c * will not ar‐
68       chive all your files; tar c . is better.)
70   Empty lists
71       The nice and simple rule given above: "expand a wildcard  pattern  into
72       the  list  of matching pathnames" was the original UNIX definition.  It
73       allowed one to have patterns that expand into an empty list, as in
75           xv -wait 0 *.gif *.jpg
77       where perhaps no *.gif files are present (and this is  not  an  error).
78       However,  POSIX requires that a wildcard pattern is left unchanged when
79       it is syntactically incorrect, or the list  of  matching  pathnames  is
80       empty.   With bash one can force the classical behavior using this com‐
81       mand:
83           shopt -s nullglob
85       (Similar problems occur elsewhere.  For example, where old scripts have
87           rm `find . -name "*~"`
89       new scripts require
91           rm -f nosuchfile `find . -name "*~"`
93       to avoid error messages from rm called with an empty argument list.)


96   Regular expressions
97       Note that wildcard patterns are not regular expressions, although  they
98       are  a  bit  similar.   First of all, they match filenames, rather than
99       text, and secondly, the conventions are not the same: for example, in a
100       regular  expression  '*'  means  zero  or  more copies of the preceding
101       thing.
103       Now that regular expressions have bracket expressions where  the  nega‐
104       tion is indicated by a '^', POSIX has declared the effect of a wildcard
105       pattern "[^...]" to be undefined.
107   Character classes and internationalization
108       Of course ranges were originally meant to  be  ASCII  ranges,  so  that
109       "[ -%]"  stands  for  "[ !"#$%]"  and "[a-z]" stands for "any lowercase
110       letter".  Some UNIX implementations generalized this so  that  a  range
111       X-Y  stands for the set of characters with code between the codes for X
112       and for Y.  However, this requires the user to know the character  cod‐
113       ing  in use on the local system, and moreover, is not convenient if the
114       collating sequence for the local alphabet differs from the ordering  of
115       the  character  codes.   Therefore, POSIX extended the bracket notation
116       greatly, both for wildcard patterns and for  regular  expressions.   In
117       the  above  we  saw  three  types  of items that can occur in a bracket
118       expression: namely (i) the negation, (ii) explicit  single  characters,
119       and  (iii)  ranges.   POSIX specifies ranges in an internationally more
120       useful way and adds three more types:
122       (iii) Ranges X-Y comprise all characters that  fall  between  X  and  Y
123       (inclusive) in the current collating sequence as defined by the LC_COL‐
124       LATE category in the current locale.
126       (iv) Named character classes, like
128       [:alnum:]  [:alpha:]  [:blank:]  [:cntrl:]
129       [:digit:]  [:graph:]  [:lower:]  [:print:]
130       [:punct:]  [:space:]  [:upper:]  [:xdigit:]
132       so that one can say "[[:lower:]]" instead of "[a-z]", and  have  things
133       work  in  Denmark,  too,  where there are three letters past 'z' in the
134       alphabet.  These character classes are defined by the LC_CTYPE category
135       in the current locale.
137       (v) Collating symbols, like "[.ch.]" or "[.a-acute.]", where the string
138       between "[." and ".]" is a collating element defined  for  the  current
139       locale.  Note that this may be a multicharacter element.
141       (vi)  Equivalence  class  expressions,  like  "[=a=]", where the string
142       between "[=" and "=]" is any collating  element  from  its  equivalence
143       class, as defined for the current locale.  For example, "[[=a=]]" might
144       be equivalent to "[aáàäâ]", that is,  to  "[a[.a-acute.][.a-grave.][.a-
145       umlaut.][.a-circumflex.]]".


148       sh(1), fnmatch(3), glob(3), locale(7), regex(7)


151       This  page  is  part of release 4.15 of the Linux man-pages project.  A
152       description of the project, information about reporting bugs,  and  the
153       latest     version     of     this    page,    can    be    found    at
154       https://www.kernel.org/doc/man-pages/.
158Linux                             2016-10-08                           GLOB(7)