1GLOB(7) Linux Programmer's Manual GLOB(7)
2
3
4
6 glob - globbing pathnames
7
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.
11
12 These days there is also a library routine glob(3) that will perform
13 this function for a user program.
14
15 The rules are as follows (POSIX.2, 3.13).
16
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:
22
23 A '?' (not between brackets) matches any single character.
24
25 A '*' (not between brackets) matches any string, including the empty
26 string.
27
28 Character classes
29
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 '!'.)
36
37 Ranges
38
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.)
45
46 Complementation
47
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 '-'.)
52
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 '\'.
58
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.)
65
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.)
69
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
74
75 xv -wait 0 *.gif *.jpg
76
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:
82
83 shopt -s nullglob
84
85 (Similar problems occur elsewhere. For example, where old scripts have
86
87 rm `find . -name "*~"`
88
89 new scripts require
90
91 rm -f nosuchfile `find . -name "*~"`
92
93 to avoid error messages from rm called with an empty argument list.)
94
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.
102
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.
106
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 ex‐
118 pression: namely (i) the negation, (ii) explicit single characters, and
119 (iii) ranges. POSIX specifies ranges in an internationally more useful
120 way and adds three more types:
121
122 (iii) Ranges X-Y comprise all characters that fall between X and Y (in‐
123 clusive) in the current collating sequence as defined by the LC_COLLATE
124 category in the current locale.
125
126 (iv) Named character classes, like
127
128 [:alnum:] [:alpha:] [:blank:] [:cntrl:]
129 [:digit:] [:graph:] [:lower:] [:print:]
130 [:punct:] [:space:] [:upper:] [:xdigit:]
131
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 al‐
134 phabet. These character classes are defined by the LC_CTYPE category
135 in the current locale.
136
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.
140
141 (vi) Equivalence class expressions, like "[=a=]", where the string be‐
142 tween "[=" 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áaäâ]", that is, to "[a[.a-acute.][.a-grave.][.a-
145 umlaut.][.a-circumflex.]]".
146
148 sh(1), fnmatch(3), glob(3), locale(7), regex(7)
149
151 This page is part of release 5.13 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/.
155
156
157
158Linux 2020-08-13 GLOB(7)