Skip to content

fs

luau
local fs = require("@lute/fs")

WARNING

These APIs are still open to future evolution. In new major versions, they may change in backwards incompatible ways.

Summary

EntryDescription
DirectoryEntryA single entry returned by fs.listdir, containing the entry's name and type.
FileHandleAn open file handle returned by fs.open.
FileMetadataMetadata about a filesystem entry, such as size, type, and timestamps.
FileTypeThe type of a filesystem entry:
HandleModeHow a file should be opened:
WatchEventA filesystem change event passed to the fs.watch callback.
WatchHandleA handle to an active filesystem watcher, returned by fs.watch.
closeCloses handle, flushing any pending writes.
copyCopies the file at src to dest.
existsReturns true if a file or directory exists at path.
linkCreates a hard link at dest pointing to src.
listdirReturns an array of DirectoryEntry values for the immediate children of the directory at path.
mkdirCreates a directory at the specified path.
moveMoves the file or directory at src to dest.
openOpens the file at path in the given mode (defaults to "r"). Returns a file handle.
readReads the full contents of handle and returns them as a string.
removeRemoves the file at path.
rmdirRemoves the directory at path.
statReturns metadata for the file or directory at path.
symlinkCreates a symbolic link at dest pointing to src.
typeReturns the FileType of the entry at path.
watchWatches path for filesystem changes, calling callback with the filename and event on each change.
writeWrites contents to handle.

Types

DirectoryEntry

A single entry returned by fs.listdir, containing the entry's name and type.

luau
type DirectoryEntry = {
	name: string,
	type: FileType,
}

FileHandle

An open file handle returned by fs.open.

luau
type FileHandle = {
	fd: number,
	err: number,
}

FileMetadata

Metadata about a filesystem entry, such as size, type, and timestamps.

luau
type FileMetadata = {
	type: FileType,
	permissions: { readonly: boolean },
	size: number,
	created: time.Duration,
	accessed: time.Duration,
	modified: time.Duration,
}

FileType

The type of a filesystem entry:

  • "file": A regular file.

  • "dir": A directory.

  • "link": A symbolic link.

  • "fifo": A named pipe (FIFO).

  • "socket": A Unix domain socket.

  • "char": A character device.

  • "block": A block device.

  • "unknown": An entry whose type could not be determined.

luau
type FileType = "file" | "dir" | "link" | "fifo" | "socket" | "char" | "block" | "unknown"

HandleMode

How a file should be opened:

  • "r": Open for reading. Fails if the file does not exist.

  • "w": Open for writing; creates the file if absent, truncates it if present.

  • "x": Open for exclusive creation. Fails if the file already exists.

  • "a": Open for appending; creates the file if absent, writes go to the end.

  • "r+": Open for reading and writing. Fails if the file does not exist.

  • "w+": Open for reading and writing; creates the file if absent, truncates it if present.

  • "x+": Open for reading and exclusive creation. Fails if the file already exists.

  • "a+": Open for reading and appending; creates the file if absent, writes go to the end.

luau
type HandleMode = "r" | "w" | "x" | "a" | "r+" | "w+" | "x+" | "a+"

WatchEvent

A filesystem change event passed to the fs.watch callback.

luau
type WatchEvent = {
	change: boolean,
	rename: boolean,
}

WatchHandle

A handle to an active filesystem watcher, returned by fs.watch.

luau
type WatchHandle = {
	close: (self: WatchHandle) -> (),
}

Functions and Properties

fs.close

Closes handle, flushing any pending writes.

luau
(handle: FileHandle) -> ()

fs.copy

Copies the file at src to dest.

luau
(src: string, dest: string) -> ()

fs.exists

Returns true if a file or directory exists at path.

luau
(path: string) -> boolean

Creates a hard link at dest pointing to src.

luau
(src: string, dest: string) -> ()

fs.listdir

Returns an array of DirectoryEntry values for the immediate children of the directory at path.

luau
(path: string) -> { DirectoryEntry }

fs.mkdir

Creates a directory at the specified path.

To set the permissions mode for a directory (Unix only), see @std/process for run or system to shell out to chmod or the equivalent.

luau
(path: string) -> ()

fs.move

Moves the file or directory at src to dest.

Falls back to a copy-then-remove if src and dest are on different filesystems.

luau
(src: string, dest: string) -> ()

fs.open

Opens the file at path in the given mode (defaults to "r"). Returns a file handle.

luau
(path: string, mode: HandleMode?) -> FileHandle

fs.read

Reads the full contents of handle and returns them as a string.

luau
(handle: FileHandle) -> string

fs.remove

Removes the file at path.

luau
(path: string) -> ()

fs.rmdir

Removes the directory at path.

luau
(path: string) -> ()

fs.stat

Returns metadata for the file or directory at path.

luau
(path: string) -> FileMetadata

Creates a symbolic link at dest pointing to src.

luau
(src: string, dest: string) -> ()

fs.type

Returns the FileType of the entry at path.

luau
(path: string) -> FileType

fs.watch

Watches path for filesystem changes, calling callback with the filename and event on each change.

luau
(path: string, callback: (filename: string, event: WatchEvent) -> ()) -> WatchHandle

fs.write

Writes contents to handle.

luau
(handle: FileHandle, contents: string) -> ()