c
Deno.FsFile
The Deno abstraction for reading and writing files.
This is the most straight forward way of handling files within Deno and is
recommended over using the discrete functions within the `Deno` namespace.
```ts
using file = await Deno.open("/foo/bar.txt", { read: true });
const fileInfo = await file.stat();
if (fileInfo.isFile) {
const buf = new Uint8Array(100);
const numberOfBytesRead = await file.read(buf); // 11 bytes
const text = new TextDecoder().decode(buf); // "hello world"
}
```
f
Deno.chmod
Changes the permission of a specific file/directory of specified path.
Ignores the process's umask.
```ts
await Deno.chmod("/path/to/file", 0o666);
```
The mode is a sequence of 3 octal numbers. The first/left-most number
specifies the permissions for the owner. The second number specifies the
permissions for the group. The last/right-most number specifies the
permissions for others. For example, with a mode of 0o764, the owner (7)
can read/write/execute, the group (6) can read/write and everyone else (4)
can read only.
| Number | Description |
| ------ | ----------- |
| 7 | read, write, and execute |
| 6 | read and write |
| 5 | read and execute |
| 4 | read only |
| 3 | write and execute |
| 2 | write only |
| 1 | execute only |
| 0 | no permission |
NOTE: This API currently throws on Windows
Requires `allow-write` permission.
f
Deno.chmodSync
Synchronously changes the permission of a specific file/directory of
specified path. Ignores the process's umask.
```ts
Deno.chmodSync("/path/to/file", 0o666);
```
For a full description, see [`Deno.chmod`](./././~/Deno.chmod).
NOTE: This API currently throws on Windows
Requires `allow-write` permission.
f
Deno.chown
Change owner of a regular file or directory.
This functionality is not available on Windows.
```ts
await Deno.chown("myFile.txt", 1000, 1002);
```
Requires `allow-write` permission.
Throws Error (not implemented) if executed on Windows.
f
Deno.chownSync
Synchronously change owner of a regular file or directory.
This functionality is not available on Windows.
```ts
Deno.chownSync("myFile.txt", 1000, 1002);
```
Requires `allow-write` permission.
Throws Error (not implemented) if executed on Windows.
f
Deno.copyFile
Copies the contents and permissions of one file to another specified path,
by default creating a new file if needed, else overwriting. Fails if target
path is a directory or is unwritable.
```ts
await Deno.copyFile("from.txt", "to.txt");
```
Requires `allow-read` permission on `fromPath`.
Requires `allow-write` permission on `toPath`.
f
Deno.copyFileSync
Synchronously copies the contents and permissions of one file to another
specified path, by default creating a new file if needed, else overwriting.
Fails if target path is a directory or is unwritable.
```ts
Deno.copyFileSync("from.txt", "to.txt");
```
Requires `allow-read` permission on `fromPath`.
Requires `allow-write` permission on `toPath`.
f
Deno.create
Creates a file if none exists or truncates an existing file and resolves to
an instance of [`Deno.FsFile`](./././~/Deno.FsFile).
```ts
const file = await Deno.create("/foo/bar.txt");
```
Requires `allow-read` and `allow-write` permissions.
f
Deno.createSync
Creates a file if none exists or truncates an existing file and returns
an instance of [`Deno.FsFile`](./././~/Deno.FsFile).
```ts
const file = Deno.createSync("/foo/bar.txt");
```
Requires `allow-read` and `allow-write` permissions.
f
Deno.link
Creates `newpath` as a hard link to `oldpath`.
```ts
await Deno.link("old/name", "new/name");
```
Requires `allow-read` and `allow-write` permissions.
f
Deno.linkSync
Synchronously creates `newpath` as a hard link to `oldpath`.
```ts
Deno.linkSync("old/name", "new/name");
```
Requires `allow-read` and `allow-write` permissions.
f
Deno.lstat
Resolves to a [`Deno.FileInfo`](./././~/Deno.FileInfo) for the specified `path`. If
`path` is a symlink, information for the symlink will be returned instead
of what it points to.
```ts
import { assert } from "jsr:@std/assert";
const fileInfo = await Deno.lstat("hello.txt");
assert(fileInfo.isFile);
```
Requires `allow-read` permission.
f
Deno.lstatSync
Synchronously returns a [`Deno.FileInfo`](./././~/Deno.FileInfo) for the specified
`path`. If `path` is a symlink, information for the symlink will be
returned instead of what it points to.
```ts
import { assert } from "jsr:@std/assert";
const fileInfo = Deno.lstatSync("hello.txt");
assert(fileInfo.isFile);
```
Requires `allow-read` permission.
f
Deno.makeTempDir
Creates a new temporary directory in the default directory for temporary
files, unless `dir` is specified. Other optional options include
prefixing and suffixing the directory name with `prefix` and `suffix`
respectively.
This call resolves to the full path to the newly created directory.
Multiple programs calling this function simultaneously will create different
directories. It is the caller's responsibility to remove the directory when
no longer needed.
```ts
const tempDirName0 = await Deno.makeTempDir(); // e.g. /tmp/2894ea76
const tempDirName1 = await Deno.makeTempDir({ prefix: 'my_temp' }); // e.g. /tmp/my_temp339c944d
```
Requires `allow-write` permission.
f
Deno.makeTempDirSync
Synchronously creates a new temporary directory in the default directory
for temporary files, unless `dir` is specified. Other optional options
include prefixing and suffixing the directory name with `prefix` and
`suffix` respectively.
The full path to the newly created directory is returned.
Multiple programs calling this function simultaneously will create different
directories. It is the caller's responsibility to remove the directory when
no longer needed.
```ts
const tempDirName0 = Deno.makeTempDirSync(); // e.g. /tmp/2894ea76
const tempDirName1 = Deno.makeTempDirSync({ prefix: 'my_temp' }); // e.g. /tmp/my_temp339c944d
```
Requires `allow-write` permission.
f
Deno.makeTempFile
Creates a new temporary file in the default directory for temporary
files, unless `dir` is specified.
Other options include prefixing and suffixing the directory name with
`prefix` and `suffix` respectively.
This call resolves to the full path to the newly created file.
Multiple programs calling this function simultaneously will create
different files. It is the caller's responsibility to remove the file when
no longer needed.
```ts
const tmpFileName0 = await Deno.makeTempFile(); // e.g. /tmp/419e0bf2
const tmpFileName1 = await Deno.makeTempFile({ prefix: 'my_temp' }); // e.g. /tmp/my_temp754d3098
```
Requires `allow-write` permission.
f
Deno.makeTempFileSync
Synchronously creates a new temporary file in the default directory for
temporary files, unless `dir` is specified.
Other options include prefixing and suffixing the directory name with
`prefix` and `suffix` respectively.
The full path to the newly created file is returned.
Multiple programs calling this function simultaneously will create
different files. It is the caller's responsibility to remove the file when
no longer needed.
```ts
const tempFileName0 = Deno.makeTempFileSync(); // e.g. /tmp/419e0bf2
const tempFileName1 = Deno.makeTempFileSync({ prefix: 'my_temp' }); // e.g. /tmp/my_temp754d3098
```
Requires `allow-write` permission.
f
Deno.mkdir
Creates a new directory with the specified path.
```ts
await Deno.mkdir("new_dir");
await Deno.mkdir("nested/directories", { recursive: true });
await Deno.mkdir("restricted_access_dir", { mode: 0o700 });
```
Defaults to throwing error if the directory already exists.
Requires `allow-write` permission.
f
Deno.mkdirSync
Synchronously creates a new directory with the specified path.
```ts
Deno.mkdirSync("new_dir");
Deno.mkdirSync("nested/directories", { recursive: true });
Deno.mkdirSync("restricted_access_dir", { mode: 0o700 });
```
Defaults to throwing error if the directory already exists.
Requires `allow-write` permission.
f
Deno.open
Open a file and resolve to an instance of [`Deno.FsFile`](./././~/Deno.FsFile). The
file does not need to previously exist if using the `create` or `createNew`
open options. The caller may have the resulting file automatically closed
by the runtime once it's out of scope by declaring the file variable with
the `using` keyword.
```ts
using file = await Deno.open("/foo/bar.txt", { read: true, write: true });
// Do work with file
```
Alternatively, the caller may manually close the resource when finished with
it.
```ts
const file = await Deno.open("/foo/bar.txt", { read: true, write: true });
// Do work with file
file.close();
```
Requires `allow-read` and/or `allow-write` permissions depending on
options.
f
Deno.openSync
Synchronously open a file and return an instance of
[`Deno.FsFile`](./././~/Deno.FsFile). The file does not need to previously exist if
using the `create` or `createNew` open options. The caller may have the
resulting file automatically closed by the runtime once it's out of scope
by declaring the file variable with the `using` keyword.
```ts
using file = Deno.openSync("/foo/bar.txt", { read: true, write: true });
// Do work with file
```
Alternatively, the caller may manually close the resource when finished with
it.
```ts
const file = Deno.openSync("/foo/bar.txt", { read: true, write: true });
// Do work with file
file.close();
```
Requires `allow-read` and/or `allow-write` permissions depending on
options.
f
Deno.readDir
Reads the directory given by `path` and returns an async iterable of
[`Deno.DirEntry`](./././~/Deno.DirEntry). The order of entries is not guaranteed.
```ts
for await (const dirEntry of Deno.readDir("/")) {
console.log(dirEntry.name);
}
```
Throws error if `path` is not a directory.
Requires `allow-read` permission.
f
Deno.readDirSync
Synchronously reads the directory given by `path` and returns an iterable
of [`Deno.DirEntry`](./././~/Deno.DirEntry). The order of entries is not guaranteed.
```ts
for (const dirEntry of Deno.readDirSync("/")) {
console.log(dirEntry.name);
}
```
Throws error if `path` is not a directory.
Requires `allow-read` permission.
f
Deno.readFile
Reads and resolves to the entire contents of a file as an array of bytes.
`TextDecoder` can be used to transform the bytes to string if required.
Reading a directory returns an empty data array.
```ts
const decoder = new TextDecoder("utf-8");
const data = await Deno.readFile("hello.txt");
console.log(decoder.decode(data));
```
Requires `allow-read` permission.
f
Deno.readFileSync
Synchronously reads and returns the entire contents of a file as an array
of bytes. `TextDecoder` can be used to transform the bytes to string if
required. Reading a directory returns an empty data array.
```ts
const decoder = new TextDecoder("utf-8");
const data = Deno.readFileSync("hello.txt");
console.log(decoder.decode(data));
```
Requires `allow-read` permission.
f
Deno.readLink
Resolves to the full path destination of the named symbolic link.
```ts
await Deno.symlink("./test.txt", "./test_link.txt");
const target = await Deno.readLink("./test_link.txt"); // full path of ./test.txt
```
Throws TypeError if called with a hard link.
Requires `allow-read` permission.
f
Deno.readLinkSync
Synchronously returns the full path destination of the named symbolic
link.
```ts
Deno.symlinkSync("./test.txt", "./test_link.txt");
const target = Deno.readLinkSync("./test_link.txt"); // full path of ./test.txt
```
Throws TypeError if called with a hard link.
Requires `allow-read` permission.
f
Deno.readTextFile
Asynchronously reads and returns the entire contents of a file as an UTF-8
decoded string. Reading a directory throws an error.
```ts
const data = await Deno.readTextFile("hello.txt");
console.log(data);
```
Requires `allow-read` permission.
f
Deno.readTextFileSync
Synchronously reads and returns the entire contents of a file as an UTF-8
decoded string. Reading a directory throws an error.
```ts
const data = Deno.readTextFileSync("hello.txt");
console.log(data);
```
Requires `allow-read` permission.
f
Deno.realPath
Resolves to the absolute normalized path, with symbolic links resolved.
```ts
// e.g. given /home/alice/file.txt and current directory /home/alice
await Deno.symlink("file.txt", "symlink_file.txt");
const realPath = await Deno.realPath("./file.txt");
const realSymLinkPath = await Deno.realPath("./symlink_file.txt");
console.log(realPath); // outputs "/home/alice/file.txt"
console.log(realSymLinkPath); // outputs "/home/alice/file.txt"
```
Requires `allow-read` permission for the target path.
Also requires `allow-read` permission for the `CWD` if the target path is
relative.
f
Deno.realPathSync
Synchronously returns absolute normalized path, with symbolic links
resolved.
```ts
// e.g. given /home/alice/file.txt and current directory /home/alice
Deno.symlinkSync("file.txt", "symlink_file.txt");
const realPath = Deno.realPathSync("./file.txt");
const realSymLinkPath = Deno.realPathSync("./symlink_file.txt");
console.log(realPath); // outputs "/home/alice/file.txt"
console.log(realSymLinkPath); // outputs "/home/alice/file.txt"
```
Requires `allow-read` permission for the target path.
Also requires `allow-read` permission for the `CWD` if the target path is
relative.
f
Deno.remove
Removes the named file or directory.
```ts
await Deno.remove("/path/to/empty_dir/or/file");
await Deno.remove("/path/to/populated_dir/or/file", { recursive: true });
```
Throws error if permission denied, path not found, or path is a non-empty
directory and the `recursive` option isn't set to `true`.
Requires `allow-write` permission.
f
Deno.removeSync
Synchronously removes the named file or directory.
```ts
Deno.removeSync("/path/to/empty_dir/or/file");
Deno.removeSync("/path/to/populated_dir/or/file", { recursive: true });
```
Throws error if permission denied, path not found, or path is a non-empty
directory and the `recursive` option isn't set to `true`.
Requires `allow-write` permission.
f
Deno.rename
Renames (moves) `oldpath` to `newpath`. Paths may be files or directories.
If `newpath` already exists and is not a directory, `rename()` replaces it.
OS-specific restrictions may apply when `oldpath` and `newpath` are in
different directories.
```ts
await Deno.rename("old/path", "new/path");
```
On Unix-like OSes, this operation does not follow symlinks at either path.
It varies between platforms when the operation throws errors, and if so
what they are. It's always an error to rename anything to a non-empty
directory.
Requires `allow-read` and `allow-write` permissions.
f
Deno.renameSync
Synchronously renames (moves) `oldpath` to `newpath`. Paths may be files or
directories. If `newpath` already exists and is not a directory,
`renameSync()` replaces it. OS-specific restrictions may apply when
`oldpath` and `newpath` are in different directories.
```ts
Deno.renameSync("old/path", "new/path");
```
On Unix-like OSes, this operation does not follow symlinks at either path.
It varies between platforms when the operation throws errors, and if so what
they are. It's always an error to rename anything to a non-empty directory.
Requires `allow-read` and `allow-write` permissions.
f
Deno.stat
Resolves to a [`Deno.FileInfo`](./././~/Deno.FileInfo) for the specified `path`. Will
always follow symlinks.
```ts
import { assert } from "jsr:@std/assert";
const fileInfo = await Deno.stat("hello.txt");
assert(fileInfo.isFile);
```
Requires `allow-read` permission.
f
Deno.statSync
Synchronously returns a [`Deno.FileInfo`](./././~/Deno.FileInfo) for the specified
`path`. Will always follow symlinks.
```ts
import { assert } from "jsr:@std/assert";
const fileInfo = Deno.statSync("hello.txt");
assert(fileInfo.isFile);
```
Requires `allow-read` permission.
f
Deno.symlink
Creates `newpath` as a symbolic link to `oldpath`.
The `options.type` parameter can be set to `"file"`, `"dir"` or `"junction"`.
This argument is only available on Windows and ignored on other platforms.
```ts
await Deno.symlink("old/name", "new/name");
```
Requires full `allow-read` and `allow-write` permissions.
f
Deno.symlinkSync
Creates `newpath` as a symbolic link to `oldpath`.
The `options.type` parameter can be set to `"file"`, `"dir"` or `"junction"`.
This argument is only available on Windows and ignored on other platforms.
```ts
Deno.symlinkSync("old/name", "new/name");
```
Requires full `allow-read` and `allow-write` permissions.
f
Deno.truncate
Truncates (or extends) the specified file, to reach the specified `len`.
If `len` is not specified then the entire file contents are truncated.
### Truncate the entire file
```ts
await Deno.truncate("my_file.txt");
```
### Truncate part of the file
```ts
const file = await Deno.makeTempFile();
await Deno.writeTextFile(file, "Hello World");
await Deno.truncate(file, 7);
const data = await Deno.readFile(file);
console.log(new TextDecoder().decode(data)); // "Hello W"
```
Requires `allow-write` permission.
f
Deno.truncateSync
Synchronously truncates (or extends) the specified file, to reach the
specified `len`. If `len` is not specified then the entire file contents
are truncated.
### Truncate the entire file
```ts
Deno.truncateSync("my_file.txt");
```
### Truncate part of the file
```ts
const file = Deno.makeTempFileSync();
Deno.writeFileSync(file, new TextEncoder().encode("Hello World"));
Deno.truncateSync(file, 7);
const data = Deno.readFileSync(file);
console.log(new TextDecoder().decode(data));
```
Requires `allow-write` permission.
f
Deno.umask
Retrieve the process umask. If `mask` is provided, sets the process umask.
This call always returns what the umask was before the call.
```ts
console.log(Deno.umask()); // e.g. 18 (0o022)
const prevUmaskValue = Deno.umask(0o077); // e.g. 18 (0o022)
console.log(Deno.umask()); // e.g. 63 (0o077)
```
This API is under consideration to determine if permissions are required to
call it.
*Note*: This API is not implemented on Windows
f
Deno.utime
Changes the access (`atime`) and modification (`mtime`) times of a file
system object referenced by `path`. Given times are either in seconds
(UNIX epoch time) or as `Date` objects.
```ts
await Deno.utime("myfile.txt", 1556495550, new Date());
```
Requires `allow-write` permission.
f
Deno.utimeSync
Synchronously changes the access (`atime`) and modification (`mtime`) times
of a file system object referenced by `path`. Given times are either in
seconds (UNIX epoch time) or as `Date` objects.
```ts
Deno.utimeSync("myfile.txt", 1556495550, new Date());
```
Requires `allow-write` permission.
f
Deno.watchFs
Watch for file system events against one or more `paths`, which can be
files or directories. These paths must exist already. One user action (e.g.
`touch test.file`) can generate multiple file system events. Likewise,
one user action can result in multiple file paths in one event (e.g. `mv
old_name.txt new_name.txt`).
The recursive option is `true` by default and, for directories, will watch
the specified directory and all sub directories.
Note that the exact ordering of the events can vary between operating
systems.
```ts
const watcher = Deno.watchFs("/");
for await (const event of watcher) {
console.log(">>>> event", event);
// { kind: "create", paths: [ "/foo.txt" ] }
}
```
Call `watcher.close()` to stop watching.
```ts
const watcher = Deno.watchFs("/");
setTimeout(() => {
watcher.close();
}, 5000);
for await (const event of watcher) {
console.log(">>>> event", event);
}
```
Requires `allow-read` permission.
f
Deno.writeFile
Write `data` to the given `path`, by default creating a new file if
needed, else overwriting.
```ts
const encoder = new TextEncoder();
const data = encoder.encode("Hello world\n");
await Deno.writeFile("hello1.txt", data); // overwrite "hello1.txt" or create it
await Deno.writeFile("hello2.txt", data, { create: false }); // only works if "hello2.txt" exists
await Deno.writeFile("hello3.txt", data, { mode: 0o777 }); // set permissions on new file
await Deno.writeFile("hello4.txt", data, { append: true }); // add data to the end of the file
```
Requires `allow-write` permission, and `allow-read` if `options.create` is
`false`.
f
Deno.writeFileSync
Synchronously write `data` to the given `path`, by default creating a new
file if needed, else overwriting.
```ts
const encoder = new TextEncoder();
const data = encoder.encode("Hello world\n");
Deno.writeFileSync("hello1.txt", data); // overwrite "hello1.txt" or create it
Deno.writeFileSync("hello2.txt", data, { create: false }); // only works if "hello2.txt" exists
Deno.writeFileSync("hello3.txt", data, { mode: 0o777 }); // set permissions on new file
Deno.writeFileSync("hello4.txt", data, { append: true }); // add data to the end of the file
```
Requires `allow-write` permission, and `allow-read` if `options.create` is
`false`.
f
Deno.writeTextFile
Write string `data` to the given `path`, by default creating a new file if
needed, else overwriting.
```ts
await Deno.writeTextFile("hello1.txt", "Hello world\n"); // overwrite "hello1.txt" or create it
```
Requires `allow-write` permission, and `allow-read` if `options.create` is
`false`.
f
Deno.writeTextFileSync
Synchronously write string `data` to the given `path`, by default creating
a new file if needed, else overwriting.
```ts
Deno.writeTextFileSync("hello1.txt", "Hello world\n"); // overwrite "hello1.txt" or create it
```
Requires `allow-write` permission, and `allow-read` if `options.create` is
`false`.
I
Deno.DirEntry
Information about a directory entry returned from [`Deno.readDir`](./././~/Deno.readDir)
and [`Deno.readDirSync`](./././~/Deno.readDirSync).
I
Deno.FileInfo
Provides information about a file and is returned by
[`Deno.stat`](./././~/Deno.stat), [`Deno.lstat`](./././~/Deno.lstat), [`Deno.statSync`](./././~/Deno.statSync),
and [`Deno.lstatSync`](./././~/Deno.lstatSync) or from calling `stat()` and `statSync()`
on an [`Deno.FsFile`](./././~/Deno.FsFile) instance.
I
Deno.FsEvent
Represents a unique file system event yielded by a
[`Deno.FsWatcher`](./././~/Deno.FsWatcher).
I
Deno.FsWatcher
Returned by [`Deno.watchFs`](./././~/Deno.watchFs). It is an async iterator yielding up
system events. To stop watching the file system by calling `.close()`
method.
I
Deno.MakeTempOptions
Options which can be set when using [`Deno.makeTempDir`](./././~/Deno.makeTempDir),
[`Deno.makeTempDirSync`](./././~/Deno.makeTempDirSync), [`Deno.makeTempFile`](./././~/Deno.makeTempFile), and
[`Deno.makeTempFileSync`](./././~/Deno.makeTempFileSync).
I
Deno.MkdirOptions
Options which can be set when using [`Deno.mkdir`](./././~/Deno.mkdir) and
[`Deno.mkdirSync`](./././~/Deno.mkdirSync).
I
I
Deno.ReadFileOptions
Options which can be set when using [`Deno.readFile`](./././~/Deno.readFile) or
[`Deno.readFileSync`](./././~/Deno.readFileSync).
I
Deno.RemoveOptions
Options which can be set when using [`Deno.remove`](./././~/Deno.remove) and
[`Deno.removeSync`](./././~/Deno.removeSync).
I
T
Deno.FsEventFlag
Additional information for FsEvent objects with the "other" kind.
- `"rescan"`: rescan notices indicate either a lapse in the events or a
change in the filesystem such that events received so far can no longer
be relied on to represent the state of the filesystem now. An
application that simply reacts to file changes may not care about this.
An application that keeps an in-memory representation of the filesystem
will need to care, and will need to refresh that representation directly
from the filesystem.