[25] | 1 | '\" |
---|
| 2 | '\" Copyright (c) 1993 The Regents of the University of California. |
---|
| 3 | '\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
---|
| 4 | '\" |
---|
| 5 | '\" See the file "license.terms" for information on usage and redistribution |
---|
| 6 | '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
| 7 | '\" |
---|
| 8 | '\" RCS: @(#) $Id: glob.n,v 1.22 2007/12/13 15:22:32 dgp Exp $ |
---|
| 9 | '\" |
---|
| 10 | .so man.macros |
---|
| 11 | .TH glob n 8.3 Tcl "Tcl Built-In Commands" |
---|
| 12 | .BS |
---|
| 13 | '\" Note: do not modify the .SH NAME line immediately below! |
---|
| 14 | .SH NAME |
---|
| 15 | glob \- Return names of files that match patterns |
---|
| 16 | .SH SYNOPSIS |
---|
| 17 | \fBglob \fR?\fIswitches\fR? \fIpattern \fR?\fIpattern ...\fR? |
---|
| 18 | .BE |
---|
| 19 | |
---|
| 20 | .SH DESCRIPTION |
---|
| 21 | .PP |
---|
| 22 | This command performs file name |
---|
| 23 | .QW globbing |
---|
| 24 | in a fashion similar to |
---|
| 25 | the csh shell. It returns a list of the files whose names match any |
---|
| 26 | of the \fIpattern\fR arguments. No particular order is guaranteed |
---|
| 27 | in the list, so if a sorted list is required the caller should use |
---|
| 28 | \fBlsort\fR. |
---|
| 29 | .LP |
---|
| 30 | If the initial arguments to \fBglob\fR start with \fB\-\fR then |
---|
| 31 | they are treated as switches. The following switches are |
---|
| 32 | currently supported: |
---|
| 33 | .TP |
---|
| 34 | \fB\-directory\fR \fIdirectory\fR |
---|
| 35 | Search for files which match the given patterns starting in the given |
---|
| 36 | \fIdirectory\fR. This allows searching of directories whose name |
---|
| 37 | contains glob-sensitive characters without the need to quote such |
---|
| 38 | characters explicitly. This option may not be used in conjunction with |
---|
| 39 | \fB\-path\fR, which is used to allow searching for complete file paths |
---|
| 40 | whose names may contain glob-sensitive characters. |
---|
| 41 | .TP |
---|
| 42 | \fB\-join\fR |
---|
| 43 | The remaining pattern arguments are treated as a single pattern |
---|
| 44 | obtained by joining the arguments with directory separators. |
---|
| 45 | .TP |
---|
| 46 | \fB\-nocomplain\fR |
---|
| 47 | Allows an empty list to be returned without error; without this |
---|
| 48 | switch an error is returned if the result list would be empty. |
---|
| 49 | .TP |
---|
| 50 | \fB\-path\fR \fIpathPrefix\fR |
---|
| 51 | Search for files with the given \fIpathPrefix\fR where the rest of the name |
---|
| 52 | matches the given patterns. This allows searching for files with names |
---|
| 53 | similar to a given file (as opposed to a directory) even when the names |
---|
| 54 | contain glob-sensitive |
---|
| 55 | characters. This option may not be used in conjunction with |
---|
| 56 | \fB\-directory\fR. For example, to find all files with the same root name |
---|
| 57 | as $path, but differing extensions, you should use \fBglob |
---|
| 58 | -path [file rootname $path] .*\fR which will work even if $path contains |
---|
| 59 | numerous glob-sensitive characters. |
---|
| 60 | .TP |
---|
| 61 | \fB\-tails\fR |
---|
| 62 | Only return the part of each file found which follows the last directory |
---|
| 63 | named in any \fB\-directory\fR or \fB\-path\fR path specification. |
---|
| 64 | Thus \fBglob -tails -directory $dir *\fR is equivalent to |
---|
| 65 | \fBset pwd [pwd] ; cd $dir ; glob *; cd $pwd\fR. For |
---|
| 66 | \fB\-path\fR specifications, the returned names will include the last |
---|
| 67 | path segment, so \fBglob -tails -path [file rootname ~/foo.tex] .*\fR |
---|
| 68 | will return paths like \fBfoo.aux foo.bib foo.tex\fR etc. |
---|
| 69 | .TP |
---|
| 70 | \fB\-types\fR \fItypeList\fR |
---|
| 71 | Only list files or directories which match \fItypeList\fR, where the items |
---|
| 72 | in the list have two forms. The first form is like the \-type option of |
---|
| 73 | the Unix find command: |
---|
| 74 | \fIb\fR (block special file), |
---|
| 75 | \fIc\fR (character special file), |
---|
| 76 | \fId\fR (directory), |
---|
| 77 | \fIf\fR (plain file), |
---|
| 78 | \fIl\fR (symbolic link), |
---|
| 79 | \fIp\fR (named pipe), |
---|
| 80 | or \fIs\fR (socket), where multiple types may be specified in the list. |
---|
| 81 | \fBGlob\fR will return all files which match at least one of the types given. |
---|
| 82 | Note that symbolic links will be returned both if \fB\-types l\fR is given, |
---|
| 83 | or if the target of a link matches the requested type. So, a link to |
---|
| 84 | a directory will be returned if \fB\-types d\fR was specified. |
---|
| 85 | .RS |
---|
| 86 | .PP |
---|
| 87 | The second form specifies types where all the types given must match. |
---|
| 88 | These are \fIr\fR, \fIw\fR, \fIx\fR as file permissions, and |
---|
| 89 | \fIreadonly\fR, \fIhidden\fR as special permission cases. On the |
---|
| 90 | Macintosh, MacOS types and creators are also supported, where any item |
---|
| 91 | which is four characters long is assumed to be a MacOS type |
---|
| 92 | (e.g. \fBTEXT\fR). Items which are of the form \fI{macintosh type XXXX}\fR |
---|
| 93 | or \fI{macintosh creator XXXX}\fR will match types or creators |
---|
| 94 | respectively. Unrecognized types, or specifications of multiple MacOS |
---|
| 95 | types/creators will signal an error. |
---|
| 96 | .PP |
---|
| 97 | The two forms may be mixed, so \fB\-types {d f r w}\fR will find all |
---|
| 98 | regular files OR directories that have both read AND write permissions. |
---|
| 99 | The following are equivalent: |
---|
| 100 | .RS |
---|
| 101 | .CS |
---|
| 102 | \fBglob \-type d *\fR |
---|
| 103 | \fBglob */\fR |
---|
| 104 | .CE |
---|
| 105 | .RE |
---|
| 106 | except that the first case doesn't return the trailing |
---|
| 107 | .QW / |
---|
| 108 | and is more platform independent. |
---|
| 109 | .RE |
---|
| 110 | .TP |
---|
| 111 | \fB\-\|\-\fR |
---|
| 112 | Marks the end of switches. The argument following this one will |
---|
| 113 | be treated as a \fIpattern\fR even if it starts with a \fB\-\fR. |
---|
| 114 | .PP |
---|
| 115 | The \fIpattern\fR arguments may contain any of the following |
---|
| 116 | special characters: |
---|
| 117 | .TP 10 |
---|
| 118 | \fB?\fR |
---|
| 119 | Matches any single character. |
---|
| 120 | .TP 10 |
---|
| 121 | \fB*\fR |
---|
| 122 | Matches any sequence of zero or more characters. |
---|
| 123 | .TP 10 |
---|
| 124 | \fB[\fIchars\fB]\fR |
---|
| 125 | Matches any single character in \fIchars\fR. If \fIchars\fR |
---|
| 126 | contains a sequence of the form \fIa\fB\-\fIb\fR then any |
---|
| 127 | character between \fIa\fR and \fIb\fR (inclusive) will match. |
---|
| 128 | .TP 10 |
---|
| 129 | \fB\e\fIx\fR |
---|
| 130 | Matches the character \fIx\fR. |
---|
| 131 | .TP 10 |
---|
| 132 | \fB{\fIa\fB,\fIb\fB,\fI...\fR} |
---|
| 133 | Matches any of the strings \fIa\fR, \fIb\fR, etc. |
---|
| 134 | .LP |
---|
| 135 | On Unix, as with csh, a |
---|
| 136 | .QW . |
---|
| 137 | at the beginning of a file's name or just after a |
---|
| 138 | .QW / |
---|
| 139 | must be matched explicitly or with a {} construct, unless the |
---|
| 140 | \fB\-types hidden\fR flag is given (since |
---|
| 141 | .QW . |
---|
| 142 | at the beginning of a file's name indicates that it is hidden). On |
---|
| 143 | other platforms, files beginning with a |
---|
| 144 | .QW . |
---|
| 145 | are handled no differently to any others, except the special directories |
---|
| 146 | .QW . |
---|
| 147 | and |
---|
| 148 | .QW .. |
---|
| 149 | which must be matched explicitly (this is to avoid a recursive pattern like |
---|
| 150 | .QW "glob -join * * * *" |
---|
| 151 | from recursing up the directory hierarchy as well as down). In addition, all |
---|
| 152 | .QW / |
---|
| 153 | characters must be matched explicitly. |
---|
| 154 | .LP |
---|
| 155 | If the first character in a \fIpattern\fR is |
---|
| 156 | .QW ~ |
---|
| 157 | then it refers to the home directory for the user whose name follows the |
---|
| 158 | .QW ~ . |
---|
| 159 | If the |
---|
| 160 | .QW ~ |
---|
| 161 | is followed immediately by |
---|
| 162 | .QW / |
---|
| 163 | then the value of the HOME environment variable is used. |
---|
| 164 | .LP |
---|
| 165 | The \fBglob\fR command differs from csh globbing in two ways. |
---|
| 166 | First, it does not sort its result list (use the \fBlsort\fR |
---|
| 167 | command if you want the list sorted). |
---|
| 168 | Second, \fBglob\fR only returns the names of files that actually |
---|
| 169 | exist; in csh no check for existence is made unless a pattern |
---|
| 170 | contains a ?, *, or [] construct. |
---|
| 171 | .LP |
---|
| 172 | When the \fBglob\fR command returns relative paths whose filenames |
---|
| 173 | start with a tilde |
---|
| 174 | .QW ~ |
---|
| 175 | (for example through \fBglob *\fR or \fBglob -tails\fR, the returned |
---|
| 176 | list will not quote the tilde with |
---|
| 177 | .QW ./ . |
---|
| 178 | This means care must be taken if those names are later to |
---|
| 179 | be used with \fBfile join\fR, to avoid them being interpreted as |
---|
| 180 | absolute paths pointing to a given user's home directory. |
---|
| 181 | .SH "PORTABILITY ISSUES" |
---|
| 182 | .PP |
---|
| 183 | \fBWindows\fR |
---|
| 184 | . |
---|
| 185 | For Windows UNC names, the servername and sharename components of the path |
---|
| 186 | may not contain ?, *, or [] constructs. On Windows NT, if \fIpattern\fR is |
---|
| 187 | of the form |
---|
| 188 | .QW \fB~\fIusername\fB@\fIdomain\fR , |
---|
| 189 | it refers to the home |
---|
| 190 | directory of the user whose account information resides on the specified NT |
---|
| 191 | domain server. Otherwise, user account information is obtained from |
---|
| 192 | the local computer. On Windows 95 and 98, \fBglob\fR accepts patterns |
---|
| 193 | like |
---|
| 194 | .QW .../ |
---|
| 195 | and |
---|
| 196 | .QW ..../ |
---|
| 197 | for successively higher up parent directories. |
---|
| 198 | .PP |
---|
| 199 | Since the backslash character has a special meaning to the glob |
---|
| 200 | command, glob patterns containing Windows style path separators need |
---|
| 201 | special care. The pattern \fIC:\e\efoo\e\e*\fR is interpreted as |
---|
| 202 | \fIC:\efoo\e*\fR where \fI\ef\fR will match the single character \fIf\fR |
---|
| 203 | and \fI\e*\fR will match the single character \fI*\fR and will not be |
---|
| 204 | interpreted as a wildcard character. One solution to this problem is |
---|
| 205 | to use the Unix style forward slash as a path separator. Windows style |
---|
| 206 | paths can be converted to Unix style paths with the command \fBfile |
---|
| 207 | join $path\fR (or \fBfile normalize $path\fR in Tcl 8.4). |
---|
| 208 | .SH EXAMPLES |
---|
| 209 | Find all the Tcl files in the current directory: |
---|
| 210 | .CS |
---|
| 211 | \fBglob\fR *.tcl |
---|
| 212 | .CE |
---|
| 213 | .PP |
---|
| 214 | Find all the Tcl files in the user's home directory, irrespective of |
---|
| 215 | what the current directory is: |
---|
| 216 | .CS |
---|
| 217 | \fBglob\fR \-directory ~ *.tcl |
---|
| 218 | .CE |
---|
| 219 | .PP |
---|
| 220 | Find all subdirectories of the current directory: |
---|
| 221 | .CS |
---|
| 222 | \fBglob\fR \-type d * |
---|
| 223 | .CE |
---|
| 224 | .PP |
---|
| 225 | Find all files whose name contains an |
---|
| 226 | .QW a , |
---|
| 227 | a |
---|
| 228 | .QW b |
---|
| 229 | or the sequence |
---|
| 230 | .QW cde : |
---|
| 231 | .CS |
---|
| 232 | \fBglob\fR \-type f *{a,b,cde}* |
---|
| 233 | .CE |
---|
| 234 | |
---|
| 235 | .SH "SEE ALSO" |
---|
| 236 | file(n) |
---|
| 237 | |
---|
| 238 | .SH KEYWORDS |
---|
| 239 | exist, file, glob, pattern |
---|