| 1 |
.\" Copyright (c) 1998 Andries Brouwer |
| 2 |
.\" |
| 3 |
.\" SPDX-License-Identifier: GPL-2.0-or-later |
| 4 |
.\" |
| 5 |
.\" 2003-08-24 fix for / by John Kristoff + joey |
| 6 |
.\" |
| 7 |
.TH glob 7 2024-06-15 "Linux man-pages 6.9.1" |
| 8 |
.SH NAME |
| 9 |
glob \- globbing pathnames |
| 10 |
.SH DESCRIPTION |
| 11 |
Long ago, in UNIX\ V6, there was a program |
| 12 |
.I /etc/glob |
| 13 |
that would expand wildcard patterns. |
| 14 |
Soon afterward this became a shell built-in. |
| 15 |
.P |
| 16 |
These days there is also a library routine |
| 17 |
.BR glob (3) |
| 18 |
that will perform this function for a user program. |
| 19 |
.P |
| 20 |
The rules are as follows (POSIX.2, 3.13). |
| 21 |
.SS Wildcard matching |
| 22 |
A string is a wildcard pattern if it contains one of the |
| 23 |
characters \[aq]?\[aq], \[aq]*\[aq], or \[aq][\[aq]. |
| 24 |
Globbing is the operation |
| 25 |
that expands a wildcard pattern into the list of pathnames |
| 26 |
matching the pattern. |
| 27 |
Matching is defined by: |
| 28 |
.P |
| 29 |
A \[aq]?\[aq] (not between brackets) matches any single character. |
| 30 |
.P |
| 31 |
A \[aq]*\[aq] (not between brackets) matches any string, |
| 32 |
including the empty string. |
| 33 |
.P |
| 34 |
.B "Character classes" |
| 35 |
.P |
| 36 |
An expression "\fI[...]\fP" where the first character after the |
| 37 |
leading \[aq][\[aq] is not an \[aq]!\[aq] matches a single character, |
| 38 |
namely any of the characters enclosed by the brackets. |
| 39 |
The string enclosed by the brackets cannot be empty; |
| 40 |
therefore \[aq]]\[aq] can be allowed between the brackets, provided |
| 41 |
that it is the first character. |
| 42 |
(Thus, "\fI[][!]\fP" matches the |
| 43 |
three characters \[aq][\[aq], \[aq]]\[aq], and \[aq]!\[aq].) |
| 44 |
.P |
| 45 |
.B Ranges |
| 46 |
.P |
| 47 |
There is one special convention: |
| 48 |
two characters separated by \[aq]\-\[aq] denote a range. |
| 49 |
(Thus, |
| 50 |
"\fI[A\-Fa\-f0\-9]\fP" is equivalent to "\fI[ABCDEFabcdef0123456789]\fP".) |
| 51 |
One may include \[aq]\-\[aq] in its literal meaning |
| 52 |
by making it the first or last character between the brackets. |
| 53 |
(Thus, |
| 54 |
"\fI[]\-]\fP" matches just the two characters \[aq]]\[aq] and \[aq]\-\[aq], |
| 55 |
and "\fI[\-\-0]\fP" matches the |
| 56 |
three characters \[aq]\-\[aq], \[aq].\[aq], and \[aq]0\[aq], |
| 57 |
since \[aq]/\[aq] cannot be matched.) |
| 58 |
.P |
| 59 |
.B Complementation |
| 60 |
.P |
| 61 |
An expression "\fI[!...]\fP" matches a single character, namely |
| 62 |
any character that is not matched by the expression obtained |
| 63 |
by removing the first \[aq]!\[aq] from it. |
| 64 |
(Thus, "\fI[!]a\-]\fP" matches any |
| 65 |
single character except \[aq]]\[aq], \[aq]a\[aq], and \[aq]\-\[aq].) |
| 66 |
.P |
| 67 |
One can remove the special meaning of \[aq]?\[aq], \[aq]*\[aq], and \[aq][\[aq] |
| 68 |
by preceding them by a backslash, |
| 69 |
or, |
| 70 |
in case this is part of a shell command line, |
| 71 |
enclosing them in quotes. |
| 72 |
Between brackets these characters stand for themselves. |
| 73 |
Thus, "\fI[[?*\[rs]]\fP" matches the |
| 74 |
four characters \[aq][\[aq], \[aq]?\[aq], \[aq]*\[aq], and \[aq]\[rs]\[aq]. |
| 75 |
.SS Pathnames |
| 76 |
Globbing is applied on each of the components of a pathname |
| 77 |
separately. |
| 78 |
A \[aq]/\[aq] in a pathname cannot be matched by a \[aq]?\[aq] or \[aq]*\[aq] |
| 79 |
wildcard, or by a range like "\fI[.\-0]\fP". |
| 80 |
A range containing an explicit \[aq]/\[aq] character is syntactically incorrect. |
| 81 |
(POSIX requires that syntactically incorrect patterns are left unchanged.) |
| 82 |
.P |
| 83 |
If a filename starts with a \[aq].\[aq], |
| 84 |
this character must be matched explicitly. |
| 85 |
(Thus, \fIrm\ *\fP will not remove .profile, and \fItar\ c\ *\fP will not |
| 86 |
archive all your files; \fItar\ c\ .\fP is better.) |
| 87 |
.SS Empty lists |
| 88 |
The nice and simple rule given above: "expand a wildcard pattern |
| 89 |
into the list of matching pathnames" was the original UNIX |
| 90 |
definition. |
| 91 |
It allowed one to have patterns that expand into |
| 92 |
an empty list, as in |
| 93 |
.P |
| 94 |
.nf |
| 95 |
xv \-wait 0 *.gif *.jpg |
| 96 |
.fi |
| 97 |
.P |
| 98 |
where perhaps no *.gif files are present (and this is not |
| 99 |
an error). |
| 100 |
However, POSIX requires that a wildcard pattern is left |
| 101 |
unchanged when it is syntactically incorrect, or the list of |
| 102 |
matching pathnames is empty. |
| 103 |
With |
| 104 |
.I bash |
| 105 |
one can force the classical behavior using this command: |
| 106 |
.P |
| 107 |
.in +4n |
| 108 |
.EX |
| 109 |
shopt \-s nullglob |
| 110 |
.EE |
| 111 |
.in |
| 112 |
.\" In Bash v1, by setting allow_null_glob_expansion=true |
| 113 |
.P |
| 114 |
(Similar problems occur elsewhere. |
| 115 |
For example, where old scripts have |
| 116 |
.P |
| 117 |
.in +4n |
| 118 |
.EX |
| 119 |
rm \`find . \-name "*\[ti]"\` |
| 120 |
.EE |
| 121 |
.in |
| 122 |
.P |
| 123 |
new scripts require |
| 124 |
.P |
| 125 |
.in +4n |
| 126 |
.EX |
| 127 |
rm \-f nosuchfile \`find . \-name "*\[ti]"\` |
| 128 |
.EE |
| 129 |
.in |
| 130 |
.P |
| 131 |
to avoid error messages from |
| 132 |
.I rm |
| 133 |
called with an empty argument list.) |
| 134 |
.SH NOTES |
| 135 |
.SS Regular expressions |
| 136 |
Note that wildcard patterns are not regular expressions, |
| 137 |
although they are a bit similar. |
| 138 |
First of all, they match |
| 139 |
filenames, rather than text, and secondly, the conventions |
| 140 |
are not the same: for example, in a regular expression \[aq]*\[aq] means zero or |
| 141 |
more copies of the preceding thing. |
| 142 |
.P |
| 143 |
Now that regular expressions have bracket expressions where |
| 144 |
the negation is indicated by a \[aq]\[ha]\[aq], POSIX has declared the |
| 145 |
effect of a wildcard pattern "\fI[\[ha]...]\fP" to be undefined. |
| 146 |
.SS Character classes and internationalization |
| 147 |
Of course ranges were originally meant to be ASCII ranges, |
| 148 |
so that "\fI[\ \-%]\fP" stands for "\fI[\ !"#$%]\fP" and "\fI[a\-z]\fP" stands |
| 149 |
for "any lowercase letter". |
| 150 |
Some UNIX implementations generalized this so that a range X\-Y |
| 151 |
stands for the set of characters with code between the codes for |
| 152 |
X and for Y. |
| 153 |
However, this requires the user to know the |
| 154 |
character coding in use on the local system, and moreover, is |
| 155 |
not convenient if the collating sequence for the local alphabet |
| 156 |
differs from the ordering of the character codes. |
| 157 |
Therefore, POSIX extended the bracket notation greatly, |
| 158 |
both for wildcard patterns and for regular expressions. |
| 159 |
In the above we saw three types of items that can occur in a bracket |
| 160 |
expression: namely (i) the negation, (ii) explicit single characters, |
| 161 |
and (iii) ranges. |
| 162 |
POSIX specifies ranges in an internationally |
| 163 |
more useful way and adds three more types: |
| 164 |
.P |
| 165 |
(iii) Ranges X\-Y comprise all characters that fall between X |
| 166 |
and Y (inclusive) in the current collating sequence as defined |
| 167 |
by the |
| 168 |
.B LC_COLLATE |
| 169 |
category in the current locale. |
| 170 |
.P |
| 171 |
(iv) Named character classes, like |
| 172 |
.P |
| 173 |
.nf |
| 174 |
[:alnum:] [:alpha:] [:blank:] [:cntrl:] |
| 175 |
[:digit:] [:graph:] [:lower:] [:print:] |
| 176 |
[:punct:] [:space:] [:upper:] [:xdigit:] |
| 177 |
.fi |
| 178 |
.P |
| 179 |
so that one can say "\fI[[:lower:]]\fP" instead of "\fI[a\-z]\fP", and have |
| 180 |
things work in Denmark, too, where there are three letters past \[aq]z\[aq] |
| 181 |
in the alphabet. |
| 182 |
These character classes are defined by the |
| 183 |
.B LC_CTYPE |
| 184 |
category |
| 185 |
in the current locale. |
| 186 |
.P |
| 187 |
(v) Collating symbols, like "\fI[.ch.]\fP" or "\fI[.a-acute.]\fP", |
| 188 |
where the string between "\fI[.\fP" and "\fI.]\fP" is a collating |
| 189 |
element defined for the current locale. |
| 190 |
Note that this may |
| 191 |
be a multicharacter element. |
| 192 |
.P |
| 193 |
(vi) Equivalence class expressions, like "\fI[=a=]\fP", |
| 194 |
where the string between "\fI[=\fP" and "\fI=]\fP" is any collating |
| 195 |
element from its equivalence class, as defined for the |
| 196 |
current locale. |
| 197 |
For example, "\fI[[=a=]]\fP" might be equivalent |
| 198 |
to "\fI[a\['a]\[`a]\[:a]\[^a]]\fP", that is, |
| 199 |
to "\fI[a[.a-acute.][.a-grave.][.a-umlaut.][.a-circumflex.]]\fP". |
| 200 |
.SH SEE ALSO |
| 201 |
.BR sh (1), |
| 202 |
.BR fnmatch (3), |
| 203 |
.BR glob (3), |
| 204 |
.BR locale (7), |
| 205 |
.BR regex (7) |