Debian Codegraph @

doc/file.n

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