OSDN Git Service

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