1 '\" te 2 .\" Copyright (c) 1992, X/Open Company Limited 3 .\" All Rights Reserved Portions Copyright (c) 1995, Sun Microsystems, Inc. 4 .\" All Rights Reserved 5 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at 6 .\" http://www.opengroup.org/bookstore/. 7 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html. 8 .\" This notice shall appear on any product containing this material. 9 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. 10 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License. 11 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner] 12 .TH FNMATCH 5 "Jun 14, 2015" 13 .SH NAME 14 fnmatch \- file name pattern matching 15 .SH DESCRIPTION 16 .LP 17 The pattern matching notation described below is used to specify patterns for 18 matching strings in the shell. Historically, pattern matching notation is 19 related to, but slightly different from, the regular expression notation. For 20 this reason, the description of the rules for this pattern matching notation is 21 based on the description of regular expression notation described on the 22 \fBregex\fR(5) manual page. 23 .SS "Patterns Matching a Single Character" 24 .LP 25 The following patterns match a single character: \fIordinary characters\fR, 26 \fIspecial pattern characters\fR and \fIpattern bracket expressions\fR. The pattern 27 bracket expression will also match a single collating element. 28 .sp 29 .LP 30 An ordinary character is a pattern that matches itself. It can be any character 31 in the supported character set except for \fINUL\fR, those special shell 32 characters that require quoting, and the following three special pattern 33 characters. Matching is based on the bit pattern used for encoding the 34 character, not on the graphic representation of the character. If any character 35 (ordinary, shell special, or pattern special) is quoted, that pattern will 36 match the character itself. The shell special characters always require 37 quoting. 38 .sp 39 .LP 40 When unquoted and outside a bracket expression, the following three characters 41 will have special meaning in the specification of patterns: 42 .sp 43 .ne 2 44 .na 45 \fB\fB?\fR \fR 46 .ad 47 .RS 6n 48 A question-mark is a pattern that will match any character. 49 .RE 50 51 .sp 52 .ne 2 53 .na 54 \fB\fB*\fR \fR 55 .ad 56 .RS 6n 57 An asterisk is a pattern that will match multiple characters, as described in 58 \fBPatterns Matching Multiple Characters\fR, below. 59 .RE 60 61 .sp 62 .ne 2 63 .na 64 \fB\fB[\fR \fR 65 .ad 66 .RS 6n 67 The open bracket will introduce a pattern bracket expression. 68 .RE 69 70 .sp 71 .LP 72 The description of basic regular expression bracket expressions on the 73 \fBregex\fR(5) manual page also applies to the pattern bracket expression, 74 except that the exclamation-mark character \fB(\fR \fB!\fR \fB)\fR replaces the 75 circumflex character (\fB^\fR) in its role in a \fInon-matching list\fR in the 76 regular expression notation. A bracket expression starting with an unquoted 77 circumflex character produces unspecified results. 78 .sp 79 .LP 80 The restriction on a circumflex in a bracket expression is to allow 81 implementations that support pattern matching using the circumflex as the 82 negation character in addition to the exclamation-mark. A portable application 83 must use something like \fB[\e^!\fR] to match either character. 84 .sp 85 .LP 86 When pattern matching is used where shell quote removal is not performed (such 87 as in the argument to the \fBfind\fR \fB-name\fR primary when \fBfind\fR is 88 being called using one of the \fBexec\fR functions, or in the \fIpattern\fR 89 argument to the \fBfnmatch\fR(3C) function, special characters can be escaped 90 to remove their special meaning by preceding them with a backslash character. 91 This escaping backslash will be discarded. The sequence \fB\e\e\fR represents 92 one literal backslash. All of the requirements and effects of quoting on 93 ordinary, shell special and special pattern characters will apply to escaping 94 in this context. 95 .sp 96 .LP 97 Both quoting and escaping are described here because pattern matching must work 98 in three separate circumstances: 99 .RS +4 100 .TP 101 .ie t \(bu 102 .el o 103 Calling directly upon the shell, such as in pathname expansion or in a 104 \fBcase\fR statement. All of the following will match the string or file 105 \fBabc\fR: 106 .sp 107 108 .sp 109 .TS 110 l l l l l 111 l l l l l . 112 \fBabc\fR \fB"abc"\fR \fBa"b"c\fR \fBa\ebc\fR \fBa[b]c\fR 113 \fBa["b"]c\fR \fBa[\eb]c\fR \fBa["\eb"]c\fR \fBa?c\fR \fBa*c\fR 114 .TE 115 116 The following will not: 117 .sp 118 119 .sp 120 .TS 121 l l l . 122 \fB"a?c"\fR \fBa\e*c\fR \fBa\e[b]c\fR 123 .TE 124 125 .RE 126 .RS +4 127 .TP 128 .ie t \(bu 129 .el o 130 Calling a utility or function without going through a shell, as described for 131 \fBfind\fR(1) and the function \fBfnmatch\fR(3C) 132 .RE 133 .RS +4 134 .TP 135 .ie t \(bu 136 .el o 137 Calling utilities such as \fBfind\fR, \fBcpio\fR, \fBtar\fR or \fBpax\fR 138 through the shell command line. In this case, shell quote removal is performed 139 before the utility sees the argument. For example, in: 140 .sp 141 find /bin -name e\ec[\eh]o -print 142 .sp 143 after quote removal, the backslashes are presented to \fBfind\fR and it treats 144 them as escape characters. Both precede ordinary characters, so the \fBc\fR and 145 \fBh\fR represent themselves and \fBecho\fR would be found on many historical 146 systems (that have it in \fB/bin\fR). To find a file name that contained shell 147 special characters or pattern characters, both quoting and escaping are 148 required, such as: 149 .sp 150 \fBpax -r .\|.\|. "*a\e\|(\|\e?"\fR 151 .sp 152 to extract a filename ending with \fBa(?\fR. 153 .RE 154 .sp 155 .LP 156 Conforming applications are required to quote or escape the shell special 157 characters (sometimes called metacharacters). If used without this protection, 158 syntax errors can result or implementation extensions can be triggered. For 159 example, the KornShell supports a series of extensions based on parentheses in 160 patterns; see \fBksh\fR(1) 161 .SS "Patterns Matching Multiple Characters" 162 .LP 163 The following rules are used to construct \fIpatterns matching multiple 164 characters\fR from \fIpatterns matching a single character\fR: 165 .RS +4 166 .TP 167 .ie t \(bu 168 .el o 169 The asterisk (*) is a pattern that will match any string, including the null 170 string. 171 .RE 172 .RS +4 173 .TP 174 .ie t \(bu 175 .el o 176 The concatenation of \fIpatterns matching a single character\fR is a valid 177 pattern that will match the concatenation of the single characters or collating 178 elements matched by each of the concatenated patterns. 179 .RE 180 .RS +4 181 .TP 182 .ie t \(bu 183 .el o 184 The concatenation of one or more \fIpatterns matching a single character\fR 185 with one or more asterisks is a valid pattern. In such patterns, each asterisk 186 will match a string of zero or more characters, matching the greatest possible 187 number of characters that still allows the remainder of the pattern to match 188 the string. 189 .RE 190 .sp 191 .LP 192 Since each asterisk matches zero or more occurrences, the patterns \fBa*b\fR 193 and \fBa**b\fR have identical functionality. 194 .sp 195 .LP 196 Examples: 197 .sp 198 .ne 2 199 .na 200 \fB\fBa[bc]\fR \fR 201 .ad 202 .RS 10n 203 matches the strings \fBab\fR and \fBac\fR. 204 .RE 205 206 .sp 207 .ne 2 208 .na 209 \fB\fBa*d\fR \fR 210 .ad 211 .RS 10n 212 matches the strings \fBad\fR, \fBabd\fR and \fBabcd\fR, but not the string 213 \fBabc\fR. 214 .RE 215 216 .sp 217 .ne 2 218 .na 219 \fB\fBa*d*\fR \fR 220 .ad 221 .RS 10n 222 matches the strings \fBad\fR, \fBabcd\fR, \fBabcdef\fR, \fBaaaad\fR and 223 \fBadddd\fR. 224 .RE 225 226 .sp 227 .ne 2 228 .na 229 \fB\fB*a*d\fR \fR 230 .ad 231 .RS 10n 232 matches the strings \fBad\fR, \fBabcd\fR, \fBefabcd\fR, \fBaaaad\fR and 233 \fBadddd\fR. 234 .RE 235 236 .SS "Patterns Used for Filename Expansion" 237 .LP 238 The rules described so far in \fBPatterns\fR \fBMatching\fR \fBMultiple\fR 239 \fBCharacters\fR and \fBPatterns\fR \fBMatching\fR \fBa\fR \fBSingle\fR 240 \fBCharacter\fR are qualified by the following rules that apply when pattern 241 matching notation is used for filename expansion. 242 .RS +4 243 .TP 244 1. 245 The slash character in a pathname must be explicitly matched by using one 246 or more slashes in the pattern; it cannot be matched by the asterisk or 247 question-mark special characters or by a bracket expression. Slashes in the 248 pattern are identified before bracket expressions; thus, a slash cannot be 249 included in a pattern bracket expression used for filename expansion. For 250 example, the pattern \fBa[b/c]d\fR will not match such pathnames as \fBabd\fR 251 or \fBa/d\fR. It will only match a pathname of literally \fBa[b/c]d\fR. 252 .RE 253 .RS +4 254 .TP 255 2. 256 If a filename begins with a period (.), the period must be explicitly 257 matched by using a period as the first character of the pattern or immediately 258 following a slash character. The leading period will not be matched by: 259 .sp 260 \(bu the asterisk or question-mark special characters 261 .sp 262 \(bu a bracket expression containing a non-matching list, such as: 263 .sp 264 \fB[!a]\fR 265 .sp 266 a range expression, such as: 267 .sp 268 \fB[%\(mi0]\fR 269 .sp 270 or a character class expression, such as: 271 .sp 272 \fB[[:punct:]]\fR 273 .sp 274 It is unspecified whether an explicit period in a bracket expression matching 275 list, such as: 276 .sp 277 \fB[.abc]\fR 278 .sp 279 can match a leading period in a filename. 280 .RE 281 .RS +4 282 .TP 283 3. 284 Specified patterns are matched against existing filenames and pathnames, as 285 appropriate. Each component that contains a pattern character requires read 286 permission in the directory containing that component. Any component, except 287 the last, that does not contain a pattern character requires search permission. 288 For example, given the pattern: 289 .sp 290 \fB/foo/bar/x*/bam\fR 291 .sp 292 search permission is needed for directories \fB/\fR and \fBfoo\fR, search and 293 read permissions are needed for directory \fBbar\fR, and search permission is 294 needed for each \fBx*\fR directory. 295 .sp 296 If the pattern matches any existing filenames or pathnames, the pattern will be 297 replaced with those filenames and pathnames, sorted according to the collating 298 sequence in effect in the current locale. If the pattern contains an invalid 299 bracket expression or does not match any existing filenames or pathnames, the 300 pattern string is left unchanged. 301 .RE 302 .SH SEE ALSO 303 .LP 304 \fBfind\fR(1), \fBksh\fR(1), \fBfnmatch\fR(3C), \fBregex\fR(5)