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