Skip to content

fs

read and write files, and manage folders and the current directory

Permissions: files.read, files.write

Files and folders: reading and writing them and moving them around. Every function here reads a path as relative to the current directory unless it starts from the root.

ls, rm, cp and the other file functions also work as shell-style commands, like rm -r build, which help(“shell”) explains. In an ordinary call, give the flags by name: rm("build", recursive: true).

Tags: files

  • read the contents of one or more files, as text joined into one string, or as bytes with the bytes flag
  • read_file the contents of one or more files, joined into one string
  • read_bytes a file’s contents, as bytes, for files that aren’t text
  • write_file write v to a file, replacing what was there
  • append_file add v to the end of a file, creating it if needed
  • save_session write the session’s variables, functions and units to a file as code that rebuilds them
  • exists whether a file or directory exists
  • is_dir whether path is a directory (folder)
  • ls what’s in a directory, as a table of name, type, size and modified
  • du the biggest files and folders directly in dir, largest first, with each folder counted for everything inside it
  • glob every path that matches the pattern pat, as a table like ls gives
  • file_size a file’s size, as a quantity in bytes
  • mtime when a file was last changed, as a date
  • rm delete files and empty directories
  • mv move or rename files and directories
  • cp copy files
  • mkdir create directories, along with any missing folders above them
  • abspath the full path from the root of the file system, filling in the current directory
  • cwd the current directory, which relative paths start from
  • pwd the current directory, the same as cwd() under the shell’s name
  • cd change the current directory and return the new one
read(path: str, *more: str, bytes: bool = false) -> str|bytes

the contents of one or more files, as text joined into one string, or as bytes with the bytes flag

1
read("notes.txt").lines.len
2
read("a.out", bytes: true)[..4] == b"ELF"
Example outputCtrl+Enter to run

As a command:

  • -b, --bytes give the contents as bytes, for files that aren’t text
read -b a.out
# → read("a.out", bytes: true)

See also read_file, read_bytes, write_file

read_file(path: str, *more: str) -> str

the contents of one or more files, joined into one string

Also called cat.

1
read_file("notes.txt").lines.len
2
read_file("data.json").from_json
3
cat("a.txt", "b.txt")
Example outputCtrl+Enter to run

As a command:

cat a.txt b.txt
# → cat("a.txt", "b.txt")

See also read, write_file, lines, from_csv, grep, wc

read_bytes(path: str) -> bytes

a file’s contents, as bytes, for files that aren’t text

1
read_bytes("a.out")[..4] == b"\x7fELF"
Example outputCtrl+Enter to run

See also read, read_file, bytes, hexdump

write_file(v: any, path: str) -> nil

write v to a file, replacing what was there

Also called write.

Values are written as text, a list one item per line and a table as CSV, while bytes are written exactly

1
[1, 2, 3].write_file("out.txt")
2
ls().write_file("files.csv")
Example outputCtrl+Enter to run

See also read_file, append_file

append_file(v: any, path: str) -> nil

add v to the end of a file, creating it if needed

A list goes one item per line

1
["started {now}"].append_file("log.txt")
Example outputCtrl+Enter to run

See also write_file

save_session(path: str) -> nil

write the session’s variables, functions and units to a file as code that rebuilds them

Run the file later to get them back. A value that can’t be written as code, like a table, is left as a comment

1
save_session("defs.zil")
Example outputCtrl+Enter to run

See also write_file

exists(path: str) -> bool

whether a file or directory exists

1
exists("Cargo.toml")
Example outputCtrl+Enter to run

See also is_dir

is_dir(path: str) -> bool

whether path is a directory (folder)

1
is_dir("src")
Example outputCtrl+Enter to run

See also exists

ls(dir: str = “.”, *, all: bool = false, long: bool = false) -> table

what’s in a directory, as a table of name, type, size and modified

1
ls()
2
ls("src").where(|f| f.type == "file").name
Example outputCtrl+Enter to run

As a command:

  • -a, --all include names starting with a dot, which are hidden otherwise
  • -l, --long accepted out of habit: the table always has the details
ls -la src
# → ls("src", long: true, all: true)

See also glob, sort_by, du

du(dir: str = “.”, n: int = 10) -> table

the biggest files and folders directly in dir, largest first, with each folder counted for everything inside it

A table of name, type, size and files, where files is how many files the entry holds. Hidden names are included

1
du()
2
du("src", 3).name
Example outputCtrl+Enter to run

As a command:

du src 5
# → du("src", "5")

See also ls, file_size

glob(pat: str) -> table

every path that matches the pattern pat, as a table like ls gives

* matches any part of a name, ? any one character, and ** any number of folders, so **/*.rs finds .rs files at any depth

1
glob("**/*.rs").size.sum
2
glob("*.csv").name
Example outputCtrl+Enter to run

See also ls

file_size(path: str) -> quantity

a file’s size, as a quantity in bytes

1
file_size("Cargo.lock") to KiB
Example outputCtrl+Enter to run

See also mtime, human_bytes

mtime(path: str) -> date

when a file was last changed, as a date

1
now - mtime("Cargo.lock") to h
Example outputCtrl+Enter to run

See also file_size

rm(*paths: str, recursive: bool = false, force: bool = false, dry_run: bool = false) -> table|nil

delete files and empty directories

1
rm("out.txt")
Example outputCtrl+Enter to run

As a command:

  • -r, --recursive also directories and everything in them
  • -f, --force skip paths that don’t exist instead of failing
  • -n, --dry-run list what would change, as a table of op, from and to, and change nothing
rm -rf build
# → rm("build", recursive: true, force: true)

See also mv, mkdir

mv(from: str, to: str, *more: str, dry_run: bool = false) -> table|nil

move or rename files and directories

If the last path is a directory, everything before it moves into it, so you can move several at once. Missing folders are created

1
mv("draft.txt", "final.txt")
Example outputCtrl+Enter to run

As a command:

  • -n, --dry-run list what would change, as a table of op, from and to, and change nothing
mv *.log logs
# → mv(*glob("*.log").name, "logs")

See also cp, rm

cp(from: str, to: str, *more: str, recursive: bool = false, dry_run: bool = false) -> table|nil

copy files

If the last path is a directory, everything before it is copied into it. Missing folders are created

1
cp("data.csv", "backup/data.csv")
Example outputCtrl+Enter to run

As a command:

  • -r, --recursive copy directories too
  • -n, --dry-run list what would change, as a table of op, from and to, and change nothing
cp -r src backup
# → cp("src", "backup", recursive: true)

See also mv

mkdir(path: str, *more: str, parents: bool = false, dry_run: bool = false) -> table|nil

create directories, along with any missing folders above them

1
mkdir("out/2026/10")
Example outputCtrl+Enter to run

As a command:

  • -p, --parents accepted out of habit: missing folders above are always made
  • -n, --dry-run list what would change, as a table of op, from and to, and change nothing
mkdir -p out/logs
# → mkdir("out/logs", parents: true)

See also rm

abspath(path: str) -> str

the full path from the root of the file system, filling in the current directory

The path doesn’t have to exist

1
abspath("src")
Example outputCtrl+Enter to run

See also cwd

cwd() -> str

the current directory, which relative paths start from

1
cwd()
Example outputCtrl+Enter to run

As a command:

cwd
# → cwd()

See also abspath, cd

pwd() -> str

the current directory, the same as cwd() under the shell’s name

1
pwd()
Example outputCtrl+Enter to run

As a command:

pwd
# → pwd()

See also cwd, cd

cd(dir?: str) -> str

change the current directory and return the new one

With no argument, go to your home directory, and with “-”, go back to the previous one

1
cd("src")
2
cd("..")
3
cd("-")
4
cd()
Example outputCtrl+Enter to run

As a command:

cd ../notes
# → cd("../notes")

See also cwd

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.