OSDN Git Service

Please enter the commit message for your changes. Lines starting
[eos/base.git] / util / src / TclTk / tcl8.6.12 / doc / file.n
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 .TH file n 8.3 Tcl "Tcl Built-In Commands"
9 .so man.macros
10 .BS
11 '\" Note:  do not modify the .SH NAME line immediately below!
12 .SH NAME
13 file \- Manipulate file names and attributes
14 .SH SYNOPSIS
15 \fBfile \fIoption\fR \fIname\fR ?\fIarg arg ...\fR?
16 .BE
17 .SH DESCRIPTION
18 .PP
19 This command provides several operations on a file's name or attributes.
20 \fIName\fR is the name of a file; if it starts with a tilde, then tilde
21 substitution is done before executing the command (see the manual entry for
22 \fBfilename\fR for details).  \fIOption\fR indicates what to do with the
23 file name.  Any unique abbreviation for \fIoption\fR is acceptable.  The
24 valid options are:
25 .TP
26 \fBfile atime \fIname\fR ?\fItime\fR?
27 .
28 Returns a decimal string giving the time at which file \fIname\fR was last
29 accessed.  If \fItime\fR is specified, it is an access time to set
30 for the file.  The time is measured in the standard POSIX fashion as
31 seconds from a fixed starting time (often January 1, 1970).  If the file
32 does not exist or its access time cannot be queried or set then an error is
33 generated.  On Windows, FAT file systems do not support access time.
34 .TP
35 \fBfile attributes \fIname\fR
36 .TP
37 \fBfile attributes \fIname\fR ?\fIoption\fR?
38 .TP
39 \fBfile attributes \fIname\fR ?\fIoption value option value...\fR?
40 .
41 This subcommand returns or sets platform-specific values associated
42 with a file. The first form returns a list of the platform-specific
43 options and their values. The second form returns the value for the
44 given option. The third form sets one or more of the values. The values
45 are as follows:
46 .RS
47 .PP
48 On Unix, \fB\-group\fR gets or sets the group name for the file. A
49 group id can be given to the command, but it returns a group name.
50 \fB\-owner\fR gets or sets the user name of the owner of the file. The
51 command returns the owner name, but the numerical id can be passed when
52 setting the owner. \fB\-permissions\fR retrieves or sets a file's
53 access permissions, using octal notation by default. This option also
54 provides limited support for setting permissions using the symbolic
55 notation accepted by the \fBchmod\fR command, following the form
56 [\fBugo\fR]?[[\fB+-=\fR][\fBrwxst\fR]\fB,\fR[...]]. Multiple permission
57 specifications may be given, separated by commas. E.g., \fBu+s,go-rw\fR
58 would set the setuid bit for a file's owner as well as remove read and
59 write permission for the file's group and other users. An
60 \fBls\fR-style string of the form \fBrwxrwxrwx\fR is also accepted but
61 must always be 9 characters long. E.g., \fBrwxr-xr-t\fR is equivalent
62 to \fB01755\fR. On versions of Unix supporting file flags,
63 \fB-readonly\fR returns the value of, or sets, or clears the readonly
64 attribute of a file, i.e., the user immutable flag (\fBuchg\fR) to the
65 \fBchflags\fR command.
66 .PP
67 On Windows, \fB\-archive\fR gives the value or sets or clears the
68 archive attribute of the file. \fB\-hidden\fR gives the value or sets
69 or clears the hidden attribute of the file. \fB\-longname\fR will
70 expand each path element to its long version. This attribute cannot be
71 set. \fB\-readonly\fR gives the value or sets or clears the readonly
72 attribute of the file. \fB\-shortname\fR gives a string where every
73 path element is replaced with its short (8.3) version of the
74 name. This attribute cannot be set. \fB\-system\fR gives or sets or
75 clears the value of the system attribute of the file.
76 .PP
77 On Mac OS X and Darwin, \fB\-creator\fR gives or sets the
78 Finder creator type of the file. \fB\-hidden\fR gives or sets or clears
79 the hidden attribute of the file. \fB\-readonly\fR gives or sets or
80 clears the readonly attribute of the file. \fB\-rsrclength\fR gives
81 the length of the resource fork of the file, this attribute can only be
82 set to the value 0, which results in the resource fork being stripped
83 off the file.
84 .RE
85 .TP
86 \fBfile channels\fR ?\fIpattern\fR?
87 .
88 If \fIpattern\fR is not specified, returns a list of names of all
89 registered open channels in this interpreter.  If \fIpattern\fR is
90 specified, only those names matching \fIpattern\fR are returned.  Matching
91 is determined using the same rules as for \fBstring match\fR.
92 .TP
93 \fBfile copy \fR?\fB\-force\fR? ?\fB\-\|\-\fR? \fIsource\fR \fItarget\fR
94 .TP
95 \fBfile copy \fR?\fB\-force\fR? ?\fB\-\|\-\fR? \fIsource\fR ?\fIsource\fR ...? \fItargetDir\fR
96 .
97 The first form makes a copy of the file or directory \fIsource\fR under
98 the pathname \fItarget\fR. If \fItarget\fR is an existing directory,
99 then the second form is used.  The second form makes a copy inside
100 \fItargetDir\fR of each \fIsource\fR file listed.  If a directory is
101 specified as a \fIsource\fR, then the contents of the directory will be
102 recursively copied into \fItargetDir\fR. Existing files will not be
103 overwritten unless the \fB\-force\fR option is specified (when Tcl will
104 also attempt to adjust permissions on the destination file or directory
105 if that is necessary to allow the copy to proceed).  When copying
106 within a single filesystem, \fIfile copy\fR will copy soft links (i.e.
107 the links themselves are copied, not the things they point to).  Trying
108 to overwrite a non-empty directory, overwrite a directory with a file,
109 or overwrite a file with a directory will all result in errors even if
110 \fB\-force\fR was specified.  Arguments are processed in the order
111 specified, halting at the first error, if any.  A \fB\-\|\-\fR marks
112 the end of switches; the argument following the \fB\-\|\-\fR will be
113 treated as a \fIsource\fR even if it starts with a \fB\-\fR.
114 .TP
115 \fBfile delete \fR?\fB\-force\fR? ?\fB\-\|\-\fR? ?\fIpathname\fR ... ?
116 .
117 Removes the file or directory specified by each \fIpathname\fR
118 argument.  Non-empty directories will be removed only if the
119 \fB\-force\fR option is specified.  When operating on symbolic links,
120 the links themselves will be deleted, not the objects they point to.
121 Trying to delete a non-existent file is not considered an error.
122 Trying to delete a read-only file will cause the file to be deleted,
123 even if the \fB\-force\fR flags is not specified.  If the \fB\-force\fR
124 option is specified on a directory, Tcl will attempt both to change
125 permissions and move the current directory
126 .QW pwd
127 out of the given path if that is necessary to allow the deletion to
128 proceed.  Arguments are processed in the order specified, halting at
129 the first error, if any.
130 A \fB\-\|\-\fR marks the end of switches; the argument following the
131 \fB\-\|\-\fR will be treated as a \fIpathname\fR even if it starts with
132 a \fB\-\fR.
133 .TP
134 \fBfile dirname \fIname\fR
135 Returns a name comprised of all of the path components in \fIname\fR
136 excluding the last element.  If \fIname\fR is a relative file name and
137 only contains one path element, then returns
138 .QW \fB.\fR .
139 If \fIname\fR refers to a root directory, then the root directory is
140 returned.  For example,
141 .RS
142 .PP
143 .CS
144 \fBfile dirname\fR c:/
145 .CE
146 .PP
147 returns \fBc:/\fR.
148 .PP
149 Note that tilde substitution will only be
150 performed if it is necessary to complete the command. For example,
151 .PP
152 .CS
153 \fBfile dirname\fR ~/src/foo.c
154 .CE
155 .PP
156 returns \fB~/src\fR, whereas
157 .PP
158 .CS
159 \fBfile dirname\fR ~
160 .CE
161 .PP
162 returns \fB/home\fR (or something similar).
163 .RE
164 .TP
165 \fBfile executable \fIname\fR
166 .
167 Returns \fB1\fR if file \fIname\fR is executable by the current user,
168 \fB0\fR otherwise. On Windows, which does not have an executable attribute,
169 the command treats all directories and any files with extensions
170 \fBexe\fR, \fBcom\fR, \fBcmd\fR or \fBbat\fR as executable.
171 .TP
172 \fBfile exists \fIname\fR
173 .
174 Returns \fB1\fR if file \fIname\fR exists and the current user has
175 search privileges for the directories leading to it, \fB0\fR otherwise.
176 .TP
177 \fBfile extension \fIname\fR
178 .
179 Returns all of the characters in \fIname\fR after and including the last
180 dot in the last element of \fIname\fR.  If there is no dot in the last
181 element of \fIname\fR then returns the empty string.
182 .TP
183 \fBfile isdirectory \fIname\fR
184 .
185 Returns \fB1\fR if file \fIname\fR is a directory, \fB0\fR otherwise.
186 .TP
187 \fBfile isfile \fIname\fR
188 .
189 Returns \fB1\fR if file \fIname\fR is a regular file, \fB0\fR otherwise.
190 .TP
191 \fBfile join \fIname\fR ?\fIname ...\fR?
192 .
193 Takes one or more file names and combines them, using the correct path
194 separator for the current platform.  If a particular \fIname\fR is
195 relative, then it will be joined to the previous file name argument.
196 Otherwise, any earlier arguments will be discarded, and joining will
197 proceed from the current argument.  For example,
198 .RS
199 .PP
200 .CS
201 \fBfile join\fR a b /foo bar
202 .CE
203 .PP
204 returns \fB/foo/bar\fR.
205 .PP
206 Note that any of the names can contain separators, and that the result
207 is always canonical for the current platform: \fB/\fR for Unix and
208 Windows.
209 .RE
210 .TP
211 \fBfile link\fR ?\fI\-linktype\fR? \fIlinkName\fR ?\fItarget\fR?
212 .
213 If only one argument is given, that argument is assumed to be
214 \fIlinkName\fR, and this command returns the value of the link given by
215 \fIlinkName\fR (i.e. the name of the file it points to).  If
216 \fIlinkName\fR is not a link or its value cannot be read (as, for example,
217 seems to be the case with hard links, which look just like ordinary
218 files), then an error is returned.
219 .RS
220 .PP
221 If 2 arguments are given, then these are assumed to be \fIlinkName\fR
222 and \fItarget\fR. If \fIlinkName\fR already exists, or if \fItarget\fR
223 does not exist, an error will be returned.  Otherwise, Tcl creates a new
224 link called \fIlinkName\fR which points to the existing filesystem
225 object at \fItarget\fR (which is also the returned value), where the
226 type of the link is platform-specific (on Unix a symbolic link will be
227 the default).  This is useful for the case where the user wishes to
228 create a link in a cross-platform way, and does not care what type of
229 link is created.
230 .PP
231 If the user wishes to make a link of a specific type only, (and signal an
232 error if for some reason that is not possible), then the optional
233 \fI\-linktype\fR argument should be given.  Accepted values for
234 \fI\-linktype\fR are
235 .QW \fB\-symbolic\fR
236 and
237 .QW \fB\-hard\fR .
238 .PP
239 On Unix, symbolic links can be made to relative paths, and those paths
240 must be relative to the actual \fIlinkName\fR's location (not to the
241 cwd), but on all other platforms where relative links are not supported,
242 target paths will always be converted to absolute, normalized form
243 before the link is created (and therefore relative paths are interpreted
244 as relative to the cwd).  Furthermore,
245 .QW ~user
246 paths are always expanded
247 to absolute form.  When creating links on filesystems that either do not
248 support any links, or do not support the specific type requested, an
249 error message will be returned.  Most Unix platforms support both
250 symbolic and hard links (the latter for files only). Windows
251 supports symbolic directory links and hard file links on NTFS drives.
252 .RE
253 .TP
254 \fBfile lstat \fIname varName\fR
255 .
256 Same as \fBstat\fR option (see below) except uses the \fIlstat\fR
257 kernel call instead of \fIstat\fR.  This means that if \fIname\fR
258 refers to a symbolic link the information returned in \fIvarName\fR
259 is for the link rather than the file it refers to.  On systems that
260 do not support symbolic links this option behaves exactly the same
261 as the \fBstat\fR option.
262 .TP
263 \fBfile mkdir\fR ?\fIdir\fR ...?
264 .
265 Creates each directory specified.  For each pathname \fIdir\fR specified,
266 this command will create all non-existing parent directories as
267 well as \fIdir\fR itself.  If an existing directory is specified, then
268 no action is taken and no error is returned.  Trying to overwrite an existing
269 file with a directory will result in an error.  Arguments are processed in
270 the order specified, halting at the first error, if any.
271 .TP
272 \fBfile mtime \fIname\fR ?\fItime\fR?
273 .
274 Returns a decimal string giving the time at which file \fIname\fR was last
275 modified.  If \fItime\fR is specified, it is a modification time to set for
276 the file (equivalent to Unix \fBtouch\fR).  The time is measured in the
277 standard POSIX fashion as seconds from a fixed starting time (often January
278 1, 1970).  If the file does not exist or its modified time cannot be queried
279 or set then an error is generated.
280 .TP
281 \fBfile nativename \fIname\fR
282 .
283 Returns the platform-specific name of the file. This is useful if the
284 filename is needed to pass to a platform-specific call, such as to a
285 subprocess via \fBexec\fR under Windows (see \fBEXAMPLES\fR below).
286 .TP
287 \fBfile normalize \fIname\fR
288 .
289 Returns a unique normalized path representation for the file-system
290 object (file, directory, link, etc), whose string value can be used as a
291 unique identifier for it.  A normalized path is an absolute path which has
292 all
293 .QW ../
294 and
295 .QW ./
296 removed.  Also it is one which is in the
297 .QW standard
298 format for the native platform.  On Unix, this means the segments
299 leading up to the path must be free of symbolic links/aliases (but the
300 very last path component may be a symbolic link), and on Windows it also
301 means we want the long form with that form's case-dependence (which
302 gives us a unique, case-dependent path).  The one exception concerning the
303 last link in the path is necessary, because Tcl or the user may wish to
304 operate on the actual symbolic link itself (for example \fBfile delete\fR,
305 \fBfile rename\fR, \fBfile copy\fR are defined to operate on symbolic
306 links, not on the things that they point to).
307 .TP
308 \fBfile owned \fIname\fR
309 .
310 Returns \fB1\fR if file \fIname\fR is owned by the current user, \fB0\fR
311 otherwise.
312 .TP
313 \fBfile pathtype \fIname\fR
314 .
315 Returns one of \fBabsolute\fR, \fBrelative\fR, \fBvolumerelative\fR. If
316 \fIname\fR refers to a specific file on a specific volume, the path type will
317 be \fBabsolute\fR. If \fIname\fR refers to a file relative to the current
318 working directory, then the path type will be \fBrelative\fR. If \fIname\fR
319 refers to a file relative to the current working directory on a specified
320 volume, or to a specific file on the current working volume, then the path
321 type is \fBvolumerelative\fR.
322 .TP
323 \fBfile readable \fIname\fR
324 .
325 Returns \fB1\fR if file \fIname\fR is readable by the current user,
326 \fB0\fR otherwise.
327 .TP
328 \fBfile readlink \fIname\fR
329 .
330 Returns the value of the symbolic link given by \fIname\fR (i.e. the name
331 of the file it points to).  If \fIname\fR is not a symbolic link or its
332 value cannot be read, then an error is returned.  On systems that do not
333 support symbolic links this option is undefined.
334 .TP
335 \fBfile rename \fR?\fB\-force\fR? ?\fB\-\|\-\fR? \fIsource\fR \fItarget\fR
336 .TP
337 \fBfile rename \fR?\fB\-force\fR? ?\fB\-\|\-\fR? \fIsource\fR ?\fIsource\fR ...? \fItargetDir\fR
338 .
339 The first form takes the file or directory specified by pathname
340 \fIsource\fR and renames it to \fItarget\fR, moving the file if the
341 pathname \fItarget\fR specifies a name in a different directory.  If
342 \fItarget\fR is an existing directory, then the second form is used.
343 The second form moves each \fIsource\fR file or directory into the
344 directory \fItargetDir\fR. Existing files will not be overwritten
345 unless the \fB\-force\fR option is specified.  When operating inside a
346 single filesystem, Tcl will rename symbolic links rather than the
347 things that they point to.  Trying to overwrite a non-empty directory,
348 overwrite a directory with a file, or a file with a directory will all
349 result in errors.  Arguments are processed in the order specified,
350 halting at the first error, if any.  A \fB\-\|\-\fR marks the end of
351 switches; the argument following the \fB\-\|\-\fR will be treated as a
352 \fIsource\fR even if it starts with a \fB\-\fR.
353 .TP
354 \fBfile rootname \fIname\fR
355 .
356 Returns all of the characters in \fIname\fR up to but not including the
357 last
358 .QW .
359 character in the last component of name.  If the last
360 component of \fIname\fR does not contain a dot, then returns \fIname\fR.
361 .TP
362 \fBfile separator\fR ?\fIname\fR?
363 .
364 If no argument is given, returns the character which is used to separate
365 path segments for native files on this platform.  If a path is given,
366 the filesystem responsible for that path is asked to return its
367 separator character.  If no file system accepts \fIname\fR, an error
368 is generated.
369 .TP
370 \fBfile size \fIname\fR
371 .
372 Returns a decimal string giving the size of file \fIname\fR in bytes.  If
373 the file does not exist or its size cannot be queried then an error is
374 generated.
375 .TP
376 \fBfile split \fIname\fR
377 .
378 Returns a list whose elements are the path components in \fIname\fR.  The
379 first element of the list will have the same path type as \fIname\fR.
380 All other elements will be relative.  Path separators will be discarded
381 unless they are needed to ensure that an element is unambiguously relative.
382 For example, under Unix
383 .RS
384 .PP
385 .CS
386 \fBfile split\fR /foo/~bar/baz
387 .CE
388 .PP
389 returns
390 .QW \fB/\0\0foo\0\0./~bar\0\0baz\fR
391 to ensure that later commands
392 that use the third component do not attempt to perform tilde
393 substitution.
394 .RE
395 .TP
396 \fBfile stat  \fIname varName\fR
397 .
398 Invokes the \fBstat\fR kernel call on \fIname\fR, and uses the variable
399 given by \fIvarName\fR to hold information returned from the kernel call.
400 \fIVarName\fR is treated as an array variable, and the following elements
401 of that variable are set: \fBatime\fR, \fBctime\fR, \fBdev\fR, \fBgid\fR,
402 \fBino\fR, \fBmode\fR, \fBmtime\fR, \fBnlink\fR, \fBsize\fR, \fBtype\fR,
403 \fBuid\fR.  Each element except \fBtype\fR is a decimal string with the
404 value of the corresponding field from the \fBstat\fR return structure;
405 see the manual entry for \fBstat\fR for details on the meanings of the
406 values.  The \fBtype\fR element gives the type of the file in the same
407 form returned by the command \fBfile type\fR.  This command returns an
408 empty string.
409 .TP
410 \fBfile system \fIname\fR
411 .
412 Returns a list of one or two elements, the first of which is the name of
413 the filesystem to use for the file, and the second, if given, an
414 arbitrary string representing the filesystem-specific nature or type of
415 the location within that filesystem.  If a filesystem only supports one
416 type of file, the second element may not be supplied.  For example the
417 native files have a first element
418 .QW native ,
419 and a second element which when given is a platform-specific type name
420 for the file's system (e.g.
421 .QW NTFS ,
422 .QW FAT ,
423 on Windows).  A generic virtual file system might return
424 the list
425 .QW "vfs ftp"
426 to represent a file on a remote ftp site mounted as a
427 virtual filesystem through an extension called
428 .QW vfs .
429 If the file does not belong to any filesystem, an error is generated.
430 .TP
431 \fBfile tail \fIname\fR
432 .
433 Returns all of the characters in the last filesystem component of
434 \fIname\fR.  Any trailing directory separator in \fIname\fR is ignored.
435 If \fIname\fR contains no separators then returns \fIname\fR.  So,
436 \fBfile tail a/b\fR, \fBfile tail a/b/\fR and \fBfile tail b\fR all
437 return \fBb\fR.
438 .TP
439 \fBfile tempfile\fR ?\fInameVar\fR? ?\fItemplate\fR?
440 '\" TIP #210
441 .VS 8.6
442 Creates a temporary file and returns a read-write channel opened on that file.
443 If the \fInameVar\fR is given, it specifies a variable that the name of the
444 temporary file will be written into; if absent, Tcl will attempt to arrange
445 for the temporary file to be deleted once it is no longer required. If the
446 \fItemplate\fR is present, it specifies parts of the template of the filename
447 to use when creating it (such as the directory, base-name or extension) though
448 some platforms may ignore some or all of these parts and use a built-in
449 default instead.
450 .RS
451 .PP
452 Note that temporary files are \fIonly\fR ever created on the native
453 filesystem. As such, they can be relied upon to be used with operating-system
454 native APIs and external programs that require a filename.
455 .RE
456 .VE 8.6
457 .TP
458 \fBfile type \fIname\fR
459 .
460 Returns a string giving the type of file \fIname\fR, which will be one of
461 \fBfile\fR, \fBdirectory\fR, \fBcharacterSpecial\fR, \fBblockSpecial\fR,
462 \fBfifo\fR, \fBlink\fR, or \fBsocket\fR.
463 .TP
464 \fBfile volumes\fR
465 .
466 Returns the absolute paths to the volumes mounted on the system, as a
467 proper Tcl list.  Without any virtual filesystems mounted as root
468 volumes, on UNIX, the command will always return
469 .QW / ,
470 since all filesystems are locally mounted.
471 On Windows, it will return a list of the available local drives
472 (e.g.
473 .QW "a:/ c:/" ).
474 If any virtual filesystem has mounted additional
475 volumes, they will be in the returned list.
476 .TP
477 \fBfile writable \fIname\fR
478 .
479 Returns \fB1\fR if file \fIname\fR is writable by the current user,
480 \fB0\fR otherwise.
481 .SH "PORTABILITY ISSUES"
482 .TP
483 \fBUnix\fR\0\0\0\0\0\0\0
484 .
485 These commands always operate using the real user and group identifiers,
486 not the effective ones.
487 .TP
488 \fBWindows\fR\0\0\0\0
489 .
490 The \fBfile owned\fR subcommand uses the user identifier (SID) of
491 the process token, not the thread token which may be impersonating
492 some other user.
493 .SH EXAMPLES
494 .PP
495 This procedure shows how to search for C files in a given directory
496 that have a correspondingly-named object file in the current
497 directory:
498 .PP
499 .CS
500 proc findMatchingCFiles {dir} {
501     set files {}
502     switch $::tcl_platform(platform) {
503         windows {
504             set ext .obj
505         }
506         unix {
507            set ext .o
508         }
509     }
510     foreach file [glob \-nocomplain \-directory $dir *.c] {
511         set objectFile [\fBfile tail\fR [\fBfile rootname\fR $file]]$ext
512         if {[\fBfile exists\fR $objectFile]} {
513             lappend files $file
514         }
515     }
516     return $files
517 }
518 .CE
519 .PP
520 Rename a file and leave a symbolic link pointing from the old location
521 to the new place:
522 .PP
523 .CS
524 set oldName foobar.txt
525 set newName foo/bar.txt
526 # Make sure that where we're going to move to exists...
527 if {![\fBfile isdirectory\fR [\fBfile dirname\fR $newName]]} {
528     \fBfile mkdir\fR [\fBfile dirname\fR $newName]
529 }
530 \fBfile rename\fR $oldName $newName
531 \fBfile link\fR \-symbolic $oldName $newName
532 .CE
533 .PP
534 On Windows, a file can be
535 .QW started
536 easily enough (equivalent to double-clicking on it in the Explorer
537 interface) but the name passed to the operating system must be in
538 native format:
539 .PP
540 .CS
541 exec {*}[auto_execok start] {} [\fBfile nativename\fR ~/example.txt]
542 .CE
543 .SH "SEE ALSO"
544 filename(n), open(n), close(n), eof(n), gets(n), tell(n), seek(n),
545 fblocked(n), flush(n)
546 .SH KEYWORDS
547 attributes, copy files, delete files, directory, file, move files, name,
548 rename files, stat, user
549 '\" Local Variables:
550 '\" mode: nroff
551 '\" fill-column: 78
552 '\" End: