Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/file.n @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

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