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