filefile
The commands for paths and files: names, tests, facts, and changes.
file SUBCOMMAND ?ARG ...?
Description
The commands for paths and files. The name commands join, split, dirname, tail, extension, rootname, and pathtype work on the text of a path and read no file. The other subcommands read or change the file system.
A new interpreter has no file: the host turns on the capability file. In the root folder of the interpreter (ztcl --root DIR, or an embedder with fsroot.setRoot), each path is inside that folder: / is the root folder, a relative path starts at the working folder, and a path that leaves the root folder is an error. The ztcl program uses the paths of the host by default, as tclsh does.
The separator of a path is /. A unique prefix of a subcommand is the subcommand. The examples of this page run in an empty root folder.
Subcommands
| Subcommand | Summary |
|---|---|
file join | Joins parts into one path. |
file split | Gives the parts of a path as a list. |
file dirname | Gives a path with no last part. |
file tail | Gives the last part of a path. |
file extension | Gives the extension of the last part of a path, with its dot. |
file rootname | Gives a path with no extension. |
file pathtype | Gives the kind of a path: absolute, relative, or volumerelative. |
file normalize | Gives the full path with each . and .. removed. |
file nativename | Gives a path in the form of the platform. |
file separator | Gives the separator of paths. |
file mkdir | Makes folders, with each missing parent folder. |
file exists | Tests whether a file or a folder exists. |
file isfile | Tests whether a path is a file. |
file isdirectory | Tests whether a path is a folder. |
file readable | Tests whether a file can be read. |
file writable | Tests whether a file can be written. |
file executable | Tests whether a file has a permission to run. |
file owned | Tests whether a file exists and is of the user. |
file size | Gives the size of a file in bytes. |
file type | Gives the kind of an entry of the file system: file, directory, link, and others. |
file mtime | Gives the time of the last change of a file, in seconds. |
file atime | Gives the time of the last read of a file, in seconds. |
file stat | Puts the facts of a file in an array. |
file lstat | Puts the facts of a file in an array, for a link itself. |
file attributes | Gives or sets the attributes of the platform of a file; not available in ztcl. |
file copy | Copies files and folders. |
file rename | Moves or renames files and folders. |
file delete | Removes files and folders. |
file link | Makes a link, or gives the target of a link; not available in ztcl. |
file readlink | Gives the target of a symbolic link; not available in ztcl. |
file tempfile | Opens a new temporary file and gives its channel. |
file channels | Gives the names of the open channels. |
file system | Gives the file system of a path. |
file volumes | Gives the volumes of the file system. |
Returns
The result of the subcommand.
Examples
% file join notes 2026 a.json
notes/2026/a.json
% file extension notes.json
.json
% file mkdir data
> file isdirectory data
1
# In the root folder, / is the root folder.
% file normalize a/../b/./c
/b/c
Errors
% file bogus
unknown or ambiguous subcommand "bogus": must be atime, attributes, channels, copy, delete, dirname, executable, exists, extension, isdirectory, isfile, join, link, lstat, mkdir, mtime, nativename, normalize, owned, pathtype, readable, readlink, rename, rootname, separator, size, split, stat, system, tail, tempfile, type, volumes, or writable
-errorcode TCL LOOKUP INDEX subcommand
# A path that leaves the root folder.
% file normalize ../../x
couldn't access "../../x": permission denied
-errorcode POSIX EACCES {permission denied}
Differences from Tcl
- Links, the attributes of a file, and a new time with
atimeandmtimeare errors in ztcl (issue 687). - Tcl 9.0.4 has
file home,file tempdir, andfile tildeexpand, and ztcl has none of them (issue 685). - In host mode on Windows,
pathtype,nativename,separator,volumes, andsystemgive the answers of the root folder (issue 686). - Many errors have
-errorcode NONEor a shorter code, where Tcl 8.6.16 and 9.0.4 give a code such asTCL WRONGARGSorTCL LOOKUP SUBCOMMAND(issue 658).
Facts
- Capability
file. A new interpreter does not have it until the host turns it on.- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1