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