> ## Documentation Index
> Fetch the complete documentation index at: https://axe-engine.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Read and write files

> Upload files atomically, append, create only if absent, set modes, make directories, move, copy and remove, and extract tar archives.

Uploads appear only when complete, so no reader ever sees half a file.

Every path is confined to the root directory, `--root-dir` (default `/workspace`). [Path confinement](https://axe-engine.mintlify.site/files/confinement.md) explains how paths resolve.

> [!NOTE]
>
> `axectl` has no file commands. Use the native `Files` service over gRPC, the REST API on port 8080 when the Agent Sandbox adapter is on (the default), or the [MCP file tools](https://axe-engine.mintlify.site/deploy/mcp.md#tools). All reach the same engine and follow the same rules.

The examples assume axed has no token. With one, add `-H "Authorization: Bearer $TOKEN"` to each `curl` and `grpcurl` command. See [Authentication](https://axe-engine.mintlify.site/reference/agent-sandbox-api.md#authentication).

## Upload a file

An upload writes to an unnamed temporary file (`O_TMPFILE`) in the target directory, so while bytes arrive nothing is visible in the directory. On commit, axed syncs the file. A replace-mode upload then links it under a temporary `.axe-tmp-*` name in the same directory and atomically renames that name to the target path, so the path always holds either the old file or the complete new one. A directory listing taken at that moment can briefly show the `.axe-tmp-*` entry, which already holds the complete contents; [watchers](https://axe-engine.mintlify.site/files/watch.md) never report it. A create-only upload links the file straight to the target path, with no temporary name. If the upload fails or the client vanishes, nothing is left behind. On filesystems without `O_TMPFILE`, axed writes to a named `.axe-tmp-*` file in the same directory from the start, so a listing can show it for the whole upload.

axed creates missing parent directories with mode 0755.

> [tab] gRPC
>
> `Files.Write` is a client stream: one `header`, any number of `data` chunks, then one `commit`.
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> grpcurl -plaintext -d @ localhost:9090 axe.v1.Files/Write <<'EOF'
> {"header": {"path": "notes/hello.txt", "mode": 420}}
> {"data": "aGVsbG8K"}
> {"commit": {"size": 6}}
> EOF
> ```
>
> The reply is the new file's `FileInfo`. The file appears only on `commit`. A stream that ends without one fails with `ABORTED` and leaves nothing. A clean end of stream alone never commits, because a client that vanishes mid-upload, or a proxy, can look like one. Set `commit.size` to have the commit fail with `FAILED_PRECONDITION` unless exactly that many bytes arrived.

> [tab] REST
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> curl -X PUT --data-binary @hello.txt localhost:8080/v1/files/notes/hello.txt
> curl -X PUT -F file=@hello.txt localhost:8080/v1/files/notes/hello.txt    # multipart, "file" field
> ```
>
> A successful upload returns 204 with no body.

### Modes

The mode is applied exactly, ignoring the umask, except that setuid and setgid are always dropped. The same holds for every mode axed sets: appends, new directories, copies and extracted archives.

| | Native | REST |
| - | - | - |
| Field | `WriteHeader.mode`, permission bits as a number (420 is 0644) | `?mode=0644`, exactly four octal digits starting with 0 |
| Default | 0644 | 0644 (0755 for directories and extraction) |

### Write modes

| Mode | Native `write_mode` | REST | Behavior |
| - | - | - | - |
| Replace | `WRITE_MODE_REPLACE` (default) | default | Atomically replaces whatever file is there. Writing over a directory fails with `INVALID_ARGUMENT` (400). |
| Create only | `WRITE_MODE_CREATE_ONLY` | `If-None-Match: *` header | Fails with `FAILED_PRECONDITION` (412) if anything exists at the path, including a file that appears during the upload. The commit is a single `linkat`, so of two racing uploads exactly one wins. |
| Append | `WRITE_MODE_APPEND` | `?append=true` | Appends to the file, creating it with the mode if needed. The mode does not change an existing file. |

Any other native `write_mode` value fails with `INVALID_ARGUMENT`.

> [!WARNING]
>
> Append is not atomic. axed appends bytes as they arrive, so readers see them before the commit, and a native append that ends without a commit fails with `ABORTED` but keeps the bytes it already wrote.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
curl -X PUT -H 'If-None-Match: *' --data-binary 'v2' localhost:8080/v1/files/notes/hello.txt
# {"code":"FAILED_PRECONDITION","message":"path already exists"}   412
curl -X PUT --data-binary 'more' 'localhost:8080/v1/files/notes/log.txt?append=true'
```

## Read a file

> [tab] gRPC
>
> `Files.Read` streams a `FileChunk` with the file's `info`, then its bytes in `data` chunks. `offset` and `length` read a range; `length` 0 reads to the end.
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> grpcurl -plaintext -d '{"path": "notes/hello.txt", "offset": 1, "length": 3}' \
>   localhost:9090 axe.v1.Files/Read
> ```
>
> Reading a directory fails with `FAILED_PRECONDITION`. Reading a FIFO, socket or device fails with `INVALID_ARGUMENT`.

> [tab] REST
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> curl localhost:8080/v1/files/notes/hello.txt
> curl -H 'Range: bytes=1-3' localhost:8080/v1/files/notes/hello.txt
> ```
>
> `GET` honors `Range` (several ranges come back as `multipart/byteranges`) and the conditional headers, as Go's `http.ServeContent` does.

## Inspect and list

| Task | Native | REST |
| - | - | - |
| Metadata | `Files.Stat` with `path` | `GET /v1/files/{path}?stat=true` |
| List a directory | `Files.List` with `path` and `depth` (1 for direct children, 0 for unlimited) | `GET /v1/files/{path}` lists direct children; add `?recursive=true`, optionally with `&max_depth=N` |

The native `FileInfo` has `name`, `kind` (file, directory, or other for FIFOs, sockets and devices), `size`, `mode` and `modified`. REST entries use sandboxd's field names instead; see [File metadata](https://axe-engine.mintlify.site/reference/agent-sandbox-api.md#file-metadata). Listed names are relative to the listed directory, in lexical order, with each directory before its contents. A symlink is listed with its target's metadata and is never descended into; broken or escaping links are left out. A native listing fails with `INVALID_ARGUMENT` past 100,000 entries, at any depth, including a listing of direct children. A REST listing fails with 400 past 10,000 entries when it is recursive and deeper than one level; a REST listing of direct children fails only when one directory holds more than 1,048,576 entries. Limit the depth or list a subdirectory. It also fails once the names it returns add up to more than 32 MiB. Each name is a path from the listed directory, so a very deep tree reaches that limit long before the entry limit.

## Make directories

> [tab] gRPC
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> grpcurl -plaintext -d '{"path": "a/b/c", "parents": true, "mode": 448}' \
>   localhost:9090 axe.v1.Files/MakeDir
> ```

> [tab] REST
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> curl -X PUT 'localhost:8080/v1/files/a/b/c?type=directory&parents=true&mode=0700'
> ```

Without `parents`, a missing parent fails with `NOT_FOUND`. If the directory already exists, the call succeeds and leaves it as it is. An existing file at the path fails with `ALREADY_EXISTS` (REST 409 `CONFLICT`). The default mode is 0755.

## Move, copy and remove

| Task | Native | REST |
| - | - | - |
| Move | `Files.Move` with `source`, `destination`, `overwrite` | `POST /v1/files/{source}:move?destination={path}&overwrite=true` |
| Copy | `Files.Copy` with the same fields | `POST /v1/files/{source}:copy?destination={path}&overwrite=true` |
| Remove | `Files.Remove` with `path`, `recursive` | `DELETE /v1/files/{path}?recursive=true` |

Rules for move and copy:

* axed creates missing parents of the destination.
* An existing destination fails with `ALREADY_EXISTS` (REST 409 `CONFLICT`) unless you set `overwrite`. `overwrite` replaces a file with a file, never a directory, in either direction.
* axed moves and copies symlinks as links and leaves their targets alone.
* Moving or copying a directory into itself fails with `INVALID_ARGUMENT`.
* A moved file keeps its contents and identity. A move across filesystems falls back to copy and delete.
* A copied file is written like an upload and appears only when complete. axed builds a copied directory under a temporary name and renames it into place.
* Copies keep permission bits but drop setuid and setgid.
* Copying a FIFO, socket or device fails with `INVALID_ARGUMENT`.

Rules for remove:

* Removing a symlink removes the link, never its target.
* A non-empty directory needs `recursive`; without it the call fails with `ALREADY_EXISTS` (REST 409 `CONFLICT`).
* Removing the root itself with `recursive` empties it. Without `recursive` it fails.

## Extract a tar archive

The REST API can unpack a tar or gzipped tar into a directory. The native API has no equivalent; upload the archive and run `tar` as a process instead.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
curl -X PUT --data-binary @src.tar.gz 'localhost:8080/v1/files/project?extract=tar'
```

axed creates the destination directory with `?mode` (default 0755), and any missing parents with 0755. It detects gzip from the archive's first bytes. It supports regular files, directories and symlinks, writes each file like an upload, and drops setuid and setgid bits from every mode. Directories get their archive mode plus owner `rwx`.

axed refuses, with the status shown:

| Archive content | Status |
| - | - |
| An absolute path or a `..` component | 403 `PERMISSION_DENIED` |
| An entry that would pass through a symlink, including one the archive itself created | 403 `PERMISSION_DENIED` |
| An entry whose parent path component is an existing regular file or other non-directory | 403 `PERMISSION_DENIED` |
| Hard links, devices, FIFOs and other entry types | 400 `INVALID_ARGUMENT` |
| More than 100,000 entries or more than 1 GiB of file data | 413 `RESOURCE_EXHAUSTED` |
| More than 32 KiB of header, long-name, long-link and PAX records for one entry | 413 `RESOURCE_EXHAUSTED` |
| A directory entry where a file exists, or a file or symlink entry where a directory exists | 409 `CONFLICT` |
| A corrupt archive | 400 `INVALID_ARGUMENT` |

`extract` cannot be combined with `append`, `type` or `If-None-Match`.

> [!WARNING]
>
> Extraction is not atomic as a whole. axed writes entries in order, and a refused entry stops extraction with the earlier entries already in place. Extract into a fresh directory and remove it if the request fails.

## Errors

The native API returns gRPC status codes, and the REST API returns an HTTP status with a `{"code", "message"}` body. Running out of disk space is 507 on REST. [Errors](https://axe-engine.mintlify.site/reference/errors.md) lists every mapping.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.