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

SubcommandSummary
file joinJoins parts into one path.
file splitGives the parts of a path as a list.
file dirnameGives a path with no last part.
file tailGives the last part of a path.
file extensionGives the extension of the last part of a path, with its dot.
file rootnameGives a path with no extension.
file pathtypeGives the kind of a path: absolute, relative, or volumerelative.
file normalizeGives the full path with each . and .. removed.
file nativenameGives a path in the form of the platform.
file separatorGives the separator of paths.
file mkdirMakes folders, with each missing parent folder.
file existsTests whether a file or a folder exists.
file isfileTests whether a path is a file.
file isdirectoryTests whether a path is a folder.
file readableTests whether a file can be read.
file writableTests whether a file can be written.
file executableTests whether a file has a permission to run.
file ownedTests whether a file exists and is of the user.
file sizeGives the size of a file in bytes.
file typeGives the kind of an entry of the file system: file, directory, link, and others.
file mtimeGives the time of the last change of a file, in seconds.
file atimeGives the time of the last read of a file, in seconds.
file statPuts the facts of a file in an array.
file lstatPuts the facts of a file in an array, for a link itself.
file attributesGives or sets the attributes of the platform of a file; not available in ztcl.
file copyCopies files and folders.
file renameMoves or renames files and folders.
file deleteRemoves files and folders.
file linkMakes a link, or gives the target of a link; not available in ztcl.
file readlinkGives the target of a symbolic link; not available in ztcl.
file tempfileOpens a new temporary file and gives its channel.
file channelsGives the names of the open channels.
file systemGives the file system of a path.
file volumesGives 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

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

See also