Read and change modes, owners, and times
Check and set who owns a file, who may read, write, and run it, and when it last changed.
- Read the mode and the modified time with File.stat. Both are fields of the File.Info it returns.
- Set the mode with File.chmod. It sets every bit, so build the new mode from the old one with Bits.or and Bits.and to change only a few.
- Read the owner and group ids from the
uidandgidfields, and change them with File.chown. - Set the modified time with File.touch.
- Compare two times with Time.compare.
Read the mode and the modified time
Call File.stat and read mode and modified. Print a mode in octal with String.fromInt and base: 8:
let info = try File.stat("report.txt")
println(String.fromInt(info.mode, base: 8))
println(info.modified)640
2026-11-02T08:15:00Zmodified is a Time.Instant, with no zone. Show it as a reader's clock does with Time.inZone:
let info = try File.stat("report.txt")
let zone = try Time.zone("Europe/Stockholm")
println(Time.inZone(info.modified, zone))2026-11-02T09:15:00+01:00[Europe/Stockholm]At a symbolic link, File.stat describes the file the link points to. Pass follow: false to describe the link itself; its own mode is whatever the host reports, so do not rely on it.
Test a permission bit
Mask the mode with Bits.and and compare with zero. 0o100 is the owner's execute bit, 0o002 the write bit for others:
let mode = (try File.stat("deploy.sh")).mode
let runnable = Bits.and(mode, 0o100) != 0
let worldWritable = Bits.and(mode, 0o002) != 0
println("runnable: ${runnable}, world-writable: ${worldWritable}")runnable: true, world-writable: falseThe mode does not tell whether your program can write the file: the owner, root, access control lists, and read-only mounts decide too. To find out, try the write and handle the Err.
Change the mode
Give File.chmod the whole mode, written as an octal literal. The mode also holds the setuid, setgid and sticky bits, 0o4000, 0o2000 and 0o1000; a mode outside 0 ..= 0o7777 is an Err:
try File.write("notes.txt", "draft")
try File.chmod("notes.txt", 0o600)
println(String.fromInt((try File.stat("notes.txt")).mode, base: 8))600To add bits, as chmod +x does, Bits.or them into the current mode. To clear bits, as chmod a-w does, Bits.and the mode with Bits.not of them:
func addMode(path: string, bits: int): Result[void, Error] {
let mode = (try File.stat(path)).mode
File.chmod(path, Bits.or(mode, bits))
}
func clearMode(path: string, bits: int): Result[void, Error] {
let mode = (try File.stat(path)).mode
File.chmod(path, Bits.and(mode, Bits.not(bits)))
}
try File.write("build.sh", "make\n")
try File.chmod("build.sh", 0o640)
try addMode("build.sh", 0o111)
println(String.fromInt((try File.stat("build.sh")).mode, base: 8))
try clearMode("build.sh", 0o222)
println(String.fromInt((try File.stat("build.sh")).mode, base: 8))751
551On Windows a mode only reflects the read-only attribute: File.stat reports 0o444 for a read-only file and 0o666 otherwise, and File.chmod makes the file read-only when 0o200 is clear and ignores every other bit.
A new file gets 0o666 less the process's umask, usually 0o644, so do not count on it. Writing a file that exists keeps its mode. To write a file nobody else may read, such as a token, do not write it and then call File.chmod; Replace a file without a partial write shows a file that is private from the start.
Read and change the owner
File.stat reads the owner into uid and the group into gid. They are numeric ids, as ls -n shows them, and int | null: on Windows and WebAssembly they are null, as Os.uid and Os.gid are. Give File.chown the user id and the group id; -1 keeps one as it is. A new file may take its directory's group, as it does on macOS, so this example sets the group first and then compares it:
try File.write("report.txt", "total: 42\n")
let info = try File.stat("report.txt")
println(info.uid == Os.uid())
try File.chown("report.txt", -1, Os.gid() ?? -1)
println((try File.stat("report.txt")).gid == Os.gid())true
trueOnly root may give a file to another user, and an owner may set the group only to one it belongs to; anything else is an Err. At a symbolic link File.chown changes the file the link points to; pass follow: false to change the link itself. The ids are numbers, so look a user name up first with Cmd.read(Cmd.new("id", ["-u", name])). On Windows File.chown is an Err.
Set the modified time
Pass an instant to File.touch. Without one, it sets the time to now. Either way it creates an empty file when nothing is there, and never changes what a file holds:
let release = try Time.parseInstant("2026-01-15T09:30:00Z")
try File.write("CHANGES.txt", "v1.2\n")
try File.touch("CHANGES.txt", release)
println((try File.stat("CHANGES.txt")).modified)
print(try File.read("CHANGES.txt"))2026-01-15T09:30:00Z
v1.2File.stat reads the time back rounded as the file system keeps it: to 2 seconds on FAT, to the second on HFS+, and to the nanosecond on ext4 and APFS.
File.copy copies the mode but not the times or the owner. To keep the time too, touch the copy with the original's:
let taken = try Time.parseInstant("2024-07-04T08:00:00Z")
try File.write("photo.jpg", "jpeg")
try File.touch("photo.jpg", taken)
try File.copy("photo.jpg", "backup.jpg")
try File.touch("backup.jpg", (try File.stat("photo.jpg")).modified)
println((try File.stat("backup.jpg")).modified)2024-07-04T08:00:00ZCompare modified times
< does not apply to times. Call Time.compare, which returns -1, 0, or 1, to see which file is newer:
func newer(a: string, b: string): Result[bool, Error] {
let left = try File.stat(a)
let right = try File.stat(b)
Result.ok(Time.compare(left.modified, right.modified) > 0)
}
try File.touch("site.md", try Time.parseInstant("2026-11-10T10:00:00Z"))
try File.touch("site.html", try Time.parseInstant("2026-11-09T10:00:00Z"))
println(try newer("site.md", "site.html"))trueTo find files older than a cutoff, subtract a span from an instant with Time.subtract. Use Time.now as the instant in a real script:
let now = try Time.parseInstant("2026-11-12T00:00:00Z")
let cutoff = Time.subtract(now, 168h)
for path in try File.glob("logs/*.log") {
let info = try File.stat(path)
if Time.compare(info.modified, cutoff) < 0 {
println(path)
}
}logs/b.logThe whole program
This script prepares a directory of scripts for release: it makes every .sh file in bin executable, takes write permission away from others, and lists the files changed since the last run, which it marks with a stamp file:
let stamp = ".released"
let since = (try File.stat(stamp)).modified
for name in try File.list("bin") {
let path = Path.join(["bin", name])
let info = try File.stat(path)
if name.endsWith(".sh") {
let mode = Bits.and(Bits.or(info.mode, 0o111), Bits.not(0o002))
try (File.chmod(path, mode) | Result.wrapErr("release ${path}"))
println("${path} ${String.fromInt(mode, base: 8)}")
}
if Time.compare(info.modified, since) > 0 {
println("${path} changed")
}
}
try File.touch(stamp)bin/build.sh 775
bin/deploy.sh 755
bin/deploy.sh changedBesides the functions above, it uses File.list, Path.join, String.endsWith, and Result.wrapErr.
See also
File.stat · File.Info · File.chmod · File.chown · File.touch · Os.uid · Os.gid · File.copy · File.glob · Bits.and · Bits.or · Bits.not · String.fromInt · Time.compare · Time.inZone · Time.subtract · Time.parseInstant · Replace a file without a partial write · Walk a directory and match globs
