Artifact bd6b66be9872eebfb1285b9a3822a0453aa277b0edd9c7a4c731bb7920729979:
- File doc/markdown/file.md — part of check-in [bb08aafe0a] at 2025-07-15 19:41:45 on branch tip-700 — some non-change cosmetics to make the ctags implementation in Geany 2.1 partly happy again (user: Torsten, size: 24593) [annotate] [blame] [check-ins using]
CommandName: file ManualSection: n Version: 8.3 TclPart: Tcl TclDescription: Tcl Built-In Commands Links:
- filename(n)
- open(n)
- close(n)
- eof(n)
- gets(n)
- tell(n)
- seek(n)
- fblocked(n)
- flush(n) Keywords:
- attributes
- copy files
- delete files
- directory
- file
- move files
- name
- rename files
- stat
- user Copyright:
- Copyright (c) 1993 The Regents of the University of California.
- Copyright (c) 1994-1996 Sun Microsystems, Inc. ---
Name
file - Manipulate file names and attributes
Synopsis
::: {.synopsis} ::: [file]{.cmd} [option]{.arg} [name]{.arg} [arg arg]{.optdot} :::
Description
This command provides several operations on a file's name or attributes. The fInamefR argument is the name of a file in most cases. The fIoptionfR argument indicates what to do with the file name. Any unique abbreviation for fIoptionfR is acceptable. The valid options are:
file atime fInamefR ?fItimefR? : Returns a decimal string giving the time at which file fInamefR was last accessed. If fItimefR is specified, it is an access time to set for the file. The time is measured in the standard POSIX fashion as seconds from a fixed starting time (often January 1, 1970). If the file does not exist or its access time cannot be queried or set then an error is generated. On Windows, FAT file systems do not support access time. On fBzipfsfR file systems, access time is mapped to the modification time.
file attributes fInamefR : ...see next...
file attributes fInamefR ?fIoptionfR? : ...see next...
file attributes fInamefR ?fIoption value option value...fR? : This subcommand returns or sets platform-specific values associated with a file. The first form returns a list of the platform-specific options and their values. The second form returns the value for the given option. The third form sets one or more of the values. The values are as follows: On Unix, fB-groupfR gets or sets the group name for the file. A group id can be given to the command, but it returns a group name. fB-ownerfR gets or sets the user name of the owner of the file. The command returns the owner name, but the numerical id can be passed when setting the owner. fB-permissionsfR retrieves or sets a file's access permissions, using octal notation by default. This option also provides limited support for setting permissions using the symbolic notation accepted by the fBchmodfR command, following the form [fBugofR]?[[fB+-=fR][fBrwxstfR]fB,fR[...]]. Multiple permission specifications may be given, separated by commas. E.g., fBu+s,go-rwfR would set the setuid bit for a file's owner as well as remove read and write permission for the file's group and other users. An fBlsfR-style string of the form fBrwxrwxrwxfR is also accepted but must always be 9 characters long. E.g., fBrwxr-xr-tfR is equivalent to fB01755fR. On versions of Unix supporting file flags, fB-readonlyfR returns the value of, or sets, or clears the readonly attribute of a file, i.e., the user immutable flag (fBuchgfR) to the fBchflagsfR command. On Windows, fB-archivefR gives the value or sets or clears the archive attribute of the file. fB-hiddenfR gives the value or sets or clears the hidden attribute of the file. fB-longnamefR will expand each path element to its long version. This attribute cannot be set. fB-readonlyfR gives the value or sets or clears the readonly attribute of the file. fB-shortnamefR gives a string where every path element is replaced with its short (8.3) version of the name if possible. For path elements that cannot be mapped to short names, the long name is retained. This attribute cannot be set. fB-systemfR gives or sets or clears the value of the system attribute of the file. On macOS and Darwin, fB-creatorfR gives or sets the Finder creator type of the file. fB-hiddenfR gives or sets or clears the hidden attribute of the file. fB-readonlyfR gives or sets or clears the readonly attribute of the file. fB-rsrclengthfR gives the length of the resource fork of the file, this attribute can only be set to the value 0, which results in the resource fork being stripped off the file. On all platforms, files in fBzipfsfR mounted archives return the following attributes. These are all read-only and cannot be directly set.
-archive : The path of the mounted ZIP archive containing the file.
-compsize : The compressed size of the file within the archive. This is fB0fR for directories.
-crc : The CRC of the file if present, else fB0fR.
-mount : The path where the containing archive is mounted.
-offset : The offset of the file within the archive.
-uncompsize : The uncompressed size of the file. This is fB0fR for directories.
Other attributes may be present in the returned list. These should be ignored.
file channels ?fIpatternfR? : If fIpatternfR is not specified, returns a list of names of all registered open channels in this interpreter. If fIpatternfR is specified, only those names matching fIpatternfR are returned. Matching is determined using the same rules as for fBstring matchfR.
file copy ?fB-forcefR? ?fB-|-fR? fIsource targetfR : ...see next...
file copy ?fB-forcefR? ?fB-|-fR? fIsourcefR ?fIsourcefR ...? fItargetDirfR : The first form makes a copy of the file or directory fIsourcefR under the pathname fItargetfR. If fItargetfR is an existing directory, then the second form is used. The second form makes a copy inside fItargetDirfR of each fIsourcefR file listed. If a directory is specified as a fIsourcefR, then the contents of the directory will be recursively copied into fItargetDirfR. Existing files will not be overwritten unless the fB-forcefR option is specified (when Tcl will also attempt to adjust permissions on the destination file or directory if that is necessary to allow the copy to proceed). When copying within a single filesystem, fIfile copyfR will copy soft links (i.e. the links themselves are copied, not the things they point to). Trying to overwrite a non-empty directory, overwrite a directory with a file, or overwrite a file with a directory will all result in errors even if fB-forcefR was specified. Arguments are processed in the order specified, halting at the first error, if any. A fB-|-fR marks the end of switches; the argument following the fB-|-fR will be treated as a fIsourcefR even if it starts with a fB-fR.
file delete ?fB-forcefR? ?fB-|-fR? ?fIpathnamefR ... ? : Removes the file or directory specified by each fIpathnamefR argument. Non-empty directories will be removed only if the fB-forcefR option is specified. When operating on symbolic links, the links themselves will be deleted, not the objects they point to. Trying to delete a non-existent file is not considered an error. Trying to delete a read-only file will cause the file to be deleted, even if the fB-forcefR flags is not specified. If the fB-forcefR option is specified on a directory, Tcl will attempt both to change permissions and move the current directory "pwd" out of the given path if that is necessary to allow the deletion to proceed. Arguments are processed in the order specified, halting at the first error, if any. A fB-|-fR marks the end of switches; the argument following the fB-|-fR will be treated as a fIpathnamefR even if it starts with a fB-fR.
file dirname fInamefR : Returns a name comprised of all of the path components in fInamefR excluding the last element. If fInamefR is a relative file name and only contains one path element, then returns ".". If fInamefR refers to a root directory, then the root directory is returned. For example,
```
file dirname c:/
```
returns \fBc:/\fR.
file executable fInamefR : Returns fB1fR if file fInamefR is executable by the current user, fB0fR otherwise. On Windows, which does not have an executable attribute, the command treats all directories and any files with extensions fBexefR, fBcomfR, fBcmdfR or fBbatfR as executable.
file exists fInamefR : Returns fB1fR if file fInamefR exists and the current user has search privileges for the directories leading to it, fB0fR otherwise.
file extension fInamefR : Returns all of the characters in fInamefR after and including the last dot in the last element of fInamefR. If there is no dot in the last element of fInamefR then returns the empty string.
file home ?fIusernamefR? : If no argument is specified, the command returns the home directory of the current user. This is generally the value of the fB$HOMEfR environment variable except that on Windows platforms backslashes in the path are replaced by forward slashes. An error is raised if the fB$HOMEfR environment variable is not set. If fIusernamefR is specified, the command returns the home directory configured in the system for the specified user. Note this may be different than the value of the fB$HOMEfR environment variable even when fIusernamefR corresponds to the current user. An error is raised if the fIusernamefR does not correspond to a user account on the system.
file isdirectory fInamefR : Returns fB1fR if file fInamefR is a directory, fB0fR otherwise.
file isfile fInamefR : Returns fB1fR if file fInamefR is a regular file, fB0fR otherwise.
file join fInamefR ?fIname ...fR? : Takes one or more file names and combines them, using the correct path separator for the current platform. If a particular fInamefR is relative, then it will be joined to the previous file name argument. Otherwise, any earlier arguments will be discarded, and joining will proceed from the current argument. For example,
```
file join a b /foo bar
```
returns \fB/foo/bar\fR.
Note that any of the names can contain separators, and that the result is always canonical for the current platform: \fB/\fR for Unix and Windows.
file link ?fI-linktypefR? fIlinkNamefR ?fItargetfR? : If only one argument is given, that argument is assumed to be fIlinkNamefR, and this command returns the value of the link given by fIlinkNamefR (i.e. the name of the file it points to). If fIlinkNamefR is not a link or its value cannot be read (as, for example, seems to be the case with hard links, which look just like ordinary files), then an error is returned. If 2 arguments are given, then these are assumed to be fIlinkNamefR and fItargetfR. If fIlinkNamefR already exists, or if fItargetfR does not exist, an error will be returned. Otherwise, Tcl creates a new link called fIlinkNamefR which points to the existing filesystem object at fItargetfR (which is also the returned value), where the type of the link is platform-specific (on Unix a symbolic link will be the default). This is useful for the case where the user wishes to create a link in a cross-platform way, and does not care what type of link is created. If the user wishes to make a link of a specific type only, (and signal an error if for some reason that is not possible), then the optional fI-linktypefR argument should be given. Accepted values for fI-linktypefR are "-symbolic" and "-hard". On Unix, symbolic links can be made to relative paths, and those paths must be relative to the actual fIlinkNamefR's location (not to the cwd), but on all other platforms where relative links are not supported, target paths will always be converted to absolute, normalized form before the link is created (and therefore relative paths are interpreted as relative to the cwd). When creating links on filesystems that either do not support any links, or do not support the specific type requested, an error message will be returned. Most Unix platforms support both symbolic and hard links (the latter for files only). Windows supports symbolic directory links and hard file links on NTFS drives.
file lstat fInamefR ?fIvarNamefR? : Same as fBstatfR option (see below) except uses the fIlstatfR kernel call instead of fIstatfR. This means that if fInamefR refers to a symbolic link the information returned is for the link rather than the file it refers to. On systems that do not support symbolic links this option behaves exactly the same as the fBstatfR option.
file mkdir ?fIdirfR ...? : Creates each directory specified. For each pathname fIdirfR specified, this command will create all non-existing parent directories as well as fIdirfR itself. If an existing directory is specified, then no action is taken and no error is returned. Trying to overwrite an existing file with a directory will result in an error. Arguments are processed in the order specified, halting at the first error, if any.
file mtime fInamefR ?fItimefR? : Returns a decimal string giving the time at which file fInamefR was last modified. If fItimefR is specified, it is a modification time to set for the file (equivalent to Unix fBtouchfR). The time is measured in the standard POSIX fashion as seconds from a fixed starting time (often January 1, 1970). If the file does not exist or its modified time cannot be queried or set then an error is generated. On fBzipfsfR file systems, modification time cannot be explicitly set.
file nativename fInamefR : Returns the platform-specific name of the file. This is useful if the filename is needed to pass to a platform-specific call, such as to a subprocess via fBexecfR under Windows (see fBEXAMPLESfR below).
file normalize fInamefR : Returns a unique normalized path representation for the file-system object (file, directory, link, etc), whose string value can be used as a unique identifier for it. A normalized path is an absolute path which has all "../" and "./" removed. Also it is one which is in the "standard" format for the native platform. On Unix, this means the segments leading up to the path must be free of symbolic links/aliases (but the very last path component may be a symbolic link), and on Windows it also means we want the long form with that form's case-dependence (which gives us a unique, case-dependent path). The one exception concerning the last link in the path is necessary, because Tcl or the user may wish to operate on the actual symbolic link itself (for example fBfile deletefR, fBfile renamefR, fBfile copyfR are defined to operate on symbolic links, not on the things that they point to).
file owned fInamefR : Returns fB1fR if file fInamefR is owned by the current user, fB0fR otherwise.
file pathtype fInamefR : Returns one of fBabsolutefR, fBrelativefR, fBvolumerelativefR. If fInamefR refers to a specific file on a specific volume, the path type will be fBabsolutefR. If fInamefR refers to a file relative to the current working directory, then the path type will be fBrelativefR. If fInamefR refers to a file relative to the current working directory on a specified volume, or to a specific file on the current working volume, then the path type is fBvolumerelativefR.
file readable fInamefR : Returns fB1fR if file fInamefR is readable by the current user, fB0fR otherwise.
file readlink fInamefR : Returns the value of the symbolic link given by fInamefR (i.e. the name of the file it points to). If fInamefR is not a symbolic link or its value cannot be read, then an error is returned. On systems that do not support symbolic links this option is undefined.
file rename ?fB-forcefR? ?fB-|-fR? fIsource targetfR : ...see next...
file rename ?fB-forcefR? ?fB-|-fR? fIsourcefR ?fIsourcefR ...? fItargetDirfR : The first form takes the file or directory specified by pathname fIsourcefR and renames it to fItargetfR, moving the file if the pathname fItargetfR specifies a name in a different directory. If fItargetfR is an existing directory, then the second form is used. The second form moves each fIsourcefR file or directory into the directory fItargetDirfR. Existing files will not be overwritten unless the fB-forcefR option is specified. When operating inside a single filesystem, Tcl will rename symbolic links rather than the things that they point to. Trying to overwrite a non-empty directory, overwrite a directory with a file, or a file with a directory will all result in errors. Arguments are processed in the order specified, halting at the first error, if any. A fB-|-fR marks the end of switches; the argument following the fB-|-fR will be treated as a fIsourcefR even if it starts with a fB-fR.
file rootname fInamefR : Returns all of the characters in fInamefR up to but not including the last "." character in the last component of name. If the last component of fInamefR does not contain a dot, then returns fInamefR.
file separator ?fInamefR? : If no argument is given, returns the character which is used to separate path segments for native files on this platform. If a path is given, the filesystem responsible for that path is asked to return its separator character. If no file system accepts fInamefR, an error is generated.
file size fInamefR : Returns a decimal string giving the size of file fInamefR in bytes. If the file does not exist or its size cannot be queried then an error is generated.
file split fInamefR : Returns a list whose elements are the path components in fInamefR. The first element of the list will have the same path type as fInamefR. All other elements will be relative. Path separators will be discarded unless they are needed to ensure that an element is unambiguously relative.
file stat fInamefR ?fIvarNamefR? : Invokes the fBstatfR kernel call on fInamefR, and returns a dictionary with the information returned from the kernel call. If fIvarNamefR is given, it uses the variable to hold the information. fIVarNamefR is treated as an array variable, and in such case the command returns the empty string. The following elements are set: fBatimefR, fBctimefR, fBdevfR, fBgidfR, fBinofR, fBmodefR, fBmtimefR, fBnlinkfR, fBsizefR, fBtypefR, fBuidfR. Each element except fBtypefR is a decimal string with the value of the corresponding field from the fBstatfR return structure; see the manual entry for fBstatfR for details on the meanings of the values. The fBtypefR element gives the type of the file in the same form returned by the command fBfile typefR.
file system fInamefR : Returns a list of one or two elements, the first of which is the name of the filesystem to use for the file, and the second, if given, an arbitrary string representing the filesystem-specific nature or type of the location within that filesystem. If a filesystem only supports one type of file, the second element may not be supplied. For example the native files have a first element "native", and a second element which when given is a platform-specific type name for the file's system (e.g. "NTFS", "FAT", on Windows). A generic virtual file system might return the list "vfs ftp" to represent a file on a remote ftp site mounted as a virtual filesystem through an extension called "vfs". If the file does not belong to any filesystem, an error is generated.
file tail fInamefR : Returns all of the characters in the last filesystem component of fInamefR. Any trailing directory separator in fInamefR is ignored. If fInamefR contains no separators then returns fInamefR. So, fBfile tail a/bfR, fBfile tail a/b/fR and fBfile tail bfR all return fBbfR.
file tempdir ?fItemplatefR? : Creates a temporary directory (guaranteed to be newly created and writable by the current script) and returns its name. If fItemplatefR is given, it specifies one of or both of the existing directory (on a filesystem controlled by the operating system) to contain the temporary directory, and the base part of the directory name; it is considered to have the location of the directory if there is a directory separator in the name, and the base part is everything after the last directory separator (if non-empty). The default containing directory is determined by system-specific operations, and the default base name prefix is "tcl". The following output is typical and illustrative; the actual output will vary between platforms:
```
% file tempdir
/var/tmp/tcl_u0kuy5
% file tempdir /tmp/myapp
/tmp/myapp_8o7r9L
% file tempdir /tmp/
/tmp/tcl_1mOJHD
% file tempdir myapp
/var/tmp/myapp_0ihS0n
```
file tempfile ?fInameVarfR? ?fItemplatefR? : Creates a temporary file and returns a read-write channel opened on that file. If the fInameVarfR is given, it specifies a variable that the name of the temporary file will be written into; if absent, Tcl will attempt to arrange for the temporary file to be deleted once it is no longer required. If the fItemplatefR is present, it specifies parts of the template of the filename to use when creating it (such as the directory, base-name or extension) though some platforms may ignore some or all of these parts and use a built-in default instead. Note that temporary files are fIonlyfR ever created on the native filesystem. As such, they can be relied upon to be used with operating-system native APIs and external programs that require a filename.
file tildeexpand fInamefR : Returns the result of performing tilde substitution on fInamefR. If the name begins with a tilde, then the file name will be interpreted as if the first element is replaced with the location of the home directory for the given user. If the tilde is followed immediately by a path separator, the fB$HOMEfR environment variable is substituted. Otherwise the characters between the tilde and the next separator are taken as a user name, which is used to retrieve the user's home directory for substitution. An error is raised if the fB$HOMEfR environment variable or user does not exist. If the file name does not begin with a tilde, it is returned unmodified.
file type fInamefR : Returns a string giving the type of file fInamefR, which will be one of fBfilefR, fBdirectoryfR, fBcharacterSpecialfR, fBblockSpecialfR, fBfifofR, fBlinkfR, or fBsocketfR.
file volumes : Returns the absolute paths to the volumes mounted on the system, as a proper Tcl list. Without any virtual filesystems mounted as root volumes, on UNIX, the command will always return "/", since all filesystems are locally mounted. On Windows, it will return a list of the available local drives (e.g. "a:/ c:/"). If any virtual filesystem has mounted additional volumes, they will be in the returned list.
file writable fInamefR : Returns fB1fR if file fInamefR is writable by the current user, fB0fR otherwise.
Portability issues
Unix : These commands always operate using the real user and group identifiers, not the effective ones.
Windows : The fBfile ownedfR subcommand uses the user identifier (SID) of the process token, not the thread token which may be impersonating some other user.
Examples
This procedure shows how to search for C files in a given directory that have a correspondingly-named object file in the current directory:
proc findMatchingCFiles {dir} {
set files {}
switch $::tcl_platform(platform) {
windows {
set ext .obj
}
unix {
set ext .o
}
}
foreach file [glob -nocomplain -directory $dir *.c] {
set objectFile [file tail [file rootname $file]]$ext
if {[file exists $objectFile]} {
lappend files $file
}
}
return $files
}
Rename a file and leave a symbolic link pointing from the old location to the new place:
set oldName foobar.txt
set newName foo/bar.txt
# Make sure that where we're going to move to exists...
if {![file isdirectory [file dirname $newName]]} {
file mkdir [file dirname $newName]
}
file rename $oldName $newName
file link -symbolic $oldName $newName
On Windows, a file can be "started" easily enough (equivalent to double-clicking on it in the Explorer interface) but the name passed to the operating system must be in native format:
exec {*}[auto_execok start] {} [file nativename C:/Users/fred/example.txt]