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