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 cannot contain an explicit '/' character;
63 this would lead to a syntax error.
64
65 If a filename starts with a '.', this character must be matched explic‐
66 itly. (Thus, rm * will not remove .profile, and tar c * will not ar‐
67 chive all your files; tar c . is better.)
68
69 Empty lists
70 The nice and simple rule given above: "expand a wildcard pattern into
71 the list of matching pathnames" was the original UNIX definition. It
72 allowed one to have patterns that expand into an empty list, as in
73
74 xv -wait 0 *.gif *.jpg
75
76 where perhaps no *.gif files are present (and this is not an error).
77 However, POSIX requires that a wildcard pattern is left unchanged when
78 it is syntactically incorrect, or the list of matching pathnames is
79 empty. With bash one can force the classical behavior using this com‐
80 mand:
81
82 shopt -s nullglob
83
84 (Similar problems occur elsewhere. E.g., where old scripts have
85
86 rm `find . -name "*~"`
87
88 new scripts require
89
90 rm -f nosuchfile `find . -name "*~"`
91
92 to avoid error messages from rm called with an empty argument list.)
93
95 Regular expressions
96 Note that wildcard patterns are not regular expressions, although they
97 are a bit similar. First of all, they match filenames, rather than
98 text, and secondly, the conventions are not the same: for example, in a
99 regular expression '*' means zero or more copies of the preceding
100 thing.
101
102 Now that regular expressions have bracket expressions where the nega‐
103 tion is indicated by a '^', POSIX has declared the effect of a wildcard
104 pattern "[^...]" to be undefined.
105
106 Character classes and internationalization
107 Of course ranges were originally meant to be ASCII ranges, so that
108 "[ -%]" stands for "[ !"#$%]" and "[a-z]" stands for "any lowercase
109 letter". Some UNIX implementations generalized this so that a range
110 X-Y stands for the set of characters with code between the codes for X
111 and for Y. However, this requires the user to know the character cod‐
112 ing in use on the local system, and moreover, is not convenient if the
113 collating sequence for the local alphabet differs from the ordering of
114 the character codes. Therefore, POSIX extended the bracket notation
115 greatly, both for wildcard patterns and for regular expressions. In
116 the above we saw three types of items that can occur in a bracket
117 expression: namely (i) the negation, (ii) explicit single characters,
118 and (iii) ranges. POSIX specifies ranges in an internationally more
119 useful way and adds three more types:
120
121 (iii) Ranges X-Y comprise all characters that fall between X and Y
122 (inclusive) in the current collating sequence as defined by the LC_COL‐
123 LATE category in the current locale.
124
125 (iv) Named character classes, like
126
127 [:alnum:] [:alpha:] [:blank:] [:cntrl:]
128 [:digit:] [:graph:] [:lower:] [:print:]
129 [:punct:] [:space:] [:upper:] [:xdigit:]
130
131 so that one can say "[[:lower:]]" instead of "[a-z]", and have things
132 work in Denmark, too, where there are three letters past 'z' in the
133 alphabet. These character classes are defined by the LC_CTYPE category
134 in the current locale.
135
136 (v) Collating symbols, like "[.ch.]" or "[.a-acute.]", where the string
137 between "[." and ".]" is a collating element defined for the current
138 locale. Note that this may be a multicharacter element.
139
140 (vi) Equivalence class expressions, like "[=a=]", where the string
141 between "[=" and "=]" is any collating element from its equivalence
142 class, as defined for the current locale. For example, "[[=a=]]" might
143 be equivalent to "[aáàäâ]", that is, to "[a[.a-acute.][.a-grave.][.a-
144 umlaut.][.a-circumflex.]]".
145
147 sh(1), fnmatch(3), glob(3), locale(7), regex(7)
148
150 This page is part of release 3.53 of the Linux man-pages project. A
151 description of the project, and information about reporting bugs, can
152 be found at http://www.kernel.org/doc/man-pages/.
153
154
155
156Linux 2012-07-28 GLOB(7)