OSDN Git Service

c0d0085c5f8a682e3cce666c649209233002a378
[linuxjm/LDP_man-pages.git] / original / man7 / glob.7
1 .\" Copyright (c) 1998 Andries Brouwer
2 .\"
3 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
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
20 .\" License along with this manual; if not, see
21 .\" <http://www.gnu.org/licenses/>.
22 .\" %%%LICENSE_END
23 .\"
24 .\" 2003-08-24 fix for / by John Kristoff + joey
25 .\"
26 .TH GLOB 7 2012-07-28 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 glob \- globbing pathnames
29 .SH DESCRIPTION
30 Long ago, in UNIX\ V6, there was a program
31 .I /etc/glob
32 that would expand wildcard patterns.
33 Soon afterward this became a shell built-in.
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
39 The rules are as follows (POSIX.2, 3.13).
40 .SS Wildcard matching
41 A string is a wildcard pattern if it contains one of the
42 characters \(aq?\(aq, \(aq*\(aq or \(aq[\(aq.
43 Globbing is the operation
44 that expands a wildcard pattern into the list of pathnames
45 matching the pattern.
46 Matching is defined by:
47
48 A \(aq?\(aq (not between brackets) matches any single character.
49
50 A \(aq*\(aq (not between brackets) matches any string,
51 including the empty string.
52 .PP
53 .B "Character classes"
54 .sp
55 An expression "\fI[...]\fP" where the first character after the
56 leading \(aq[\(aq is not an \(aq!\(aq matches a single character,
57 namely any of the characters enclosed by the brackets.
58 The string enclosed by the brackets cannot be empty;
59 therefore \(aq]\(aq can be allowed between the brackets, provided
60 that it is the first character.
61 (Thus, "\fI[][!]\fP" matches the
62 three characters \(aq[\(aq, \(aq]\(aq and \(aq!\(aq.)
63 .PP
64 .B Ranges
65 .sp
66 There is one special convention:
67 two characters separated by \(aq\-\(aq denote a range.
68 (Thus, "\fI[A\-Fa\-f0\-9]\fP"
69 is equivalent to "\fI[ABCDEFabcdef0123456789]\fP".)
70 One may include \(aq\-\(aq in its literal meaning by making it the
71 first or last character between the brackets.
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
75 cannot be matched.)
76 .PP
77 .B Complementation
78 .sp
79 An expression "\fI[!...]\fP" matches a single character, namely
80 any character that is not matched by the expression obtained
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.)
84
85 One can remove the special meaning of \(aq?\(aq, \(aq*\(aq and \(aq[\(aq by
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.
89 Thus, "\fI[[?*\e]\fP" matches the
90 four characters \(aq[\(aq, \(aq?\(aq, \(aq*\(aq and \(aq\e\(aq.
91 .SS Pathnames
92 Globbing is applied on each of the components of a pathname
93 separately.
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".
96 A range cannot contain an
97 explicit \(aq/\(aq character; this would lead to a syntax error.
98
99 If a filename starts with a \(aq.\(aq,
100 this character must be matched explicitly.
101 (Thus, \fIrm\ *\fP will not remove .profile, and \fItar\ c\ *\fP will not
102 archive all your files; \fItar\ c\ .\fP is better.)
103 .SS Empty lists
104 The nice and simple rule given above: "expand a wildcard pattern
105 into the list of matching pathnames" was the original UNIX
106 definition.
107 It allowed one to have patterns that expand into
108 an empty list, as in
109
110 .nf
111     xv \-wait 0 *.gif *.jpg
112 .fi
113
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
121 one can force the classical behavior using this command:
122
123     shopt \-s nullglob
124 .\" In Bash v1, by setting allow_null_glob_expansion=true
125
126 (Similar problems occur elsewhere.
127 For example, where old scripts have
128
129 .nf
130     rm \`find . \-name "*~"\`
131 .fi
132
133 new scripts require
134
135 .nf
136     rm \-f nosuchfile \`find . \-name "*~"\`
137 .fi
138
139 to avoid error messages from
140 .I rm
141 called with an empty argument list.)
142 .SH NOTES
143 .SS Regular expressions
144 Note that wildcard patterns are not regular expressions,
145 although they are a bit similar.
146 First of all, they match
147 filenames, rather than text, and secondly, the conventions
148 are not the same: for example, in a regular expression \(aq*\(aq means zero or
149 more copies of the preceding thing.
150
151 Now that regular expressions have bracket expressions where
152 the negation is indicated by a \(aq^\(aq, POSIX has declared the
153 effect of a wildcard pattern "\fI[^...]\fP" to be undefined.
154 .SS Character classes and internationalization
155 Of course ranges were originally meant to be ASCII ranges,
156 so that "\fI[\ \-%]\fP" stands for "\fI[\ !"#$%]\fP" and "\fI[a\-z]\fP" stands
157 for "any lowercase letter".
158 Some UNIX implementations generalized this so that a range X\-Y
159 stands for the set of characters with code between the codes for
160 X and for Y.
161 However, this requires the user to know the
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,
169 and (iii) ranges.
170 POSIX specifies ranges in an internationally
171 more useful way and adds three more types:
172
173 (iii) Ranges X\-Y comprise all characters that fall between X
174 and Y (inclusive) in the current collating sequence as defined
175 by the
176 .B LC_COLLATE
177 category in the current locale.
178
179 (iv) Named character classes, like
180 .nf
181
182 [:alnum:]  [:alpha:]  [:blank:]  [:cntrl:]
183 [:digit:]  [:graph:]  [:lower:]  [:print:]
184 [:punct:]  [:space:]  [:upper:]  [:xdigit:]
185
186 .fi
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
189 in the alphabet.
190 These character classes are defined by the
191 .B LC_CTYPE
192 category
193 in the current locale.
194
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
197 element defined for the current locale.
198 Note that this may
199 be a multicharacter element.
200
201 (vi) Equivalence class expressions, like "\fI[=a=]\fP",
202 where the string between "\fI[=\fP" and "\fI=]\fP" is any collating
203 element from its equivalence class, as defined for the
204 current locale.
205 For example, "\fI[[=a=]]\fP" might be equivalent
206 to "\fI[a\('a\(`a\(:a\(^a]\fP", that is,
207 to "\fI[a[.a-acute.][.a-grave.][.a-umlaut.][.a-circumflex.]]\fP".
208 .SH SEE ALSO
209 .BR sh (1),
210 .BR fnmatch (3),
211 .BR glob (3),
212 .BR locale (7),
213 .BR regex (7)
214 .SH COLOPHON
215 This page is part of release 3.67 of the Linux
216 .I man-pages
217 project.
218 A description of the project,
219 information about reporting bugs,
220 and the latest version of this page,
221 can be found at
222 \%http://www.kernel.org/doc/man\-pages/.