Skip to content

OS

@std/os manages files and directories on the host filesystem. For reading or writing file contents, use IO. For process information and the working directory, use System.

import os from "@std/os"
let entries = os.readDir(".")
for entry in entries {
println(entry.name, entry.isDir)
}
FunctionReturnsBehavior
readDir(dir: string)[]DirEntryRead directory entries, sorted by filename.
copy(src: string, dst: string)voidCopy a file’s bytes, creating or truncating the destination.
move(src: string, dst: string)voidRename or move a path using the host OS.
remove(path: string)voidRemove a file or directory tree recursively.
exists(path: string)boolTrue when the path can be stat’ed successfully.
mkdir(path: string)voidCreate directories recursively, requesting mode 0755.

Paths are interpreted by the host OS relative to the process working directory, not automatically relative to __file__. Copying does not recursively copy directories or preserve all metadata. Moving does not create destination parents and may fail across filesystems.

These functions generally raise catchable Nubo errors on host failures. exists instead returns false for any failed stat, including permission errors; it is not a detailed diagnostic.

DirEntry has these members:

MemberTypeMeaning
namestringEntry basename.
isDirboolWhether this entry is a directory.
info()FileInfoLoad its file metadata; this call can fail independently.

FileInfo contains:

FieldTypeMeaning
namestringBasename.
isDirboolDirectory flag.
sizeintSize in bytes.
modeintHost file mode, including its mode bits.
modTimeTimeTime instance supporting format(...).
import os from "@std/os"
let entries = os.readDir(".")
for entry in entries {
let info = entry.info()
println(entry.name, info.size, info.modTime.format("Y-m-d H:i:s"))
}

This example creates its source file before copying it and creates the destination directory before moving it:

import io from "@std/io"
import os from "@std/os"
import system from "@std/system"
let folder = system.cwd() + "/nubo-demo"
os.mkdir(folder + "/archive")
io.writeFile(folder + "/input.txt", "Hello, Nubo")
os.copy(folder + "/input.txt", folder + "/backup.txt")
os.move(folder + "/backup.txt", folder + "/archive/backup.txt")
println(os.exists(folder + "/archive/backup.txt"))

To remove the example directory and everything inside it:

import os from "@std/os"
os.remove("nubo-demo")
import os from "@std/os"
catch err {
let entries = os.readDir("directory-that-does-not-exist")
println(entries)
}
if !isNil(err) {
println(err.message)
}

Choosing nubo.NewWithFS(...) in Go changes script loading. It does not redirect this package’s host filesystem operations.