# Work with files

Read and change files on a machine with the file tools. A change to an existing file names the
hash of the version you read, so a file someone changed in between is never overwritten.

**In short:** `fs_stat {"path": "~/app/config.toml", "hash": true}` → `fs_edit {"path": …, "old": …, "new": …,
"expect_sha256": "<sha256>"}`. A new file: `fs_write {"path": …, "data": …, "create_only": true}`.

## Before you start

| To | Needs the class |
|---|---|
| list, stat, search, read | `read` |
| write and edit | `write` |
| delete | `destructive` |

Paths are absolute, or relative to the user's home folder; `~` is the home folder.

## Find and read

1. List a folder with [`fs_list`](https://docs.cmdop.ai/reference/tools/fs_list.md) (500 entries by default), or search
   under one with [`fs_search`](https://docs.cmdop.ai/reference/tools/fs_search.md) by `name` (`*.rs`) and plain-text
   `content`.
2. Read with [`fs_read`](https://docs.cmdop.ai/reference/tools/fs_read.md): text comes back as text, anything else as
   base64. Up to 256 KiB per call by default, 4 MiB at most; when `truncated` is true, read on with
   `offset`.

## Change a file safely

1. Get the file's hash: [`fs_stat`](https://docs.cmdop.ai/reference/tools/fs_stat.md) with `"hash": true` returns
   `sha256`. After your own write, the `sha256` that `fs_write` or `fs_edit` returned is the hash of
   the new version.
2. Change it:
   - a few exact lines: [`fs_edit`](https://docs.cmdop.ai/reference/tools/fs_edit.md) with `old`, `new` and
     `expect_sha256`. `old` must occur exactly once, whitespace included, unless you set
     `replace_all`. Text files up to 4 MiB.
   - the whole file: [`fs_write`](https://docs.cmdop.ai/reference/tools/fs_write.md) with `data` and `expect_sha256`. Up
     to 8 MiB, text or base64; the file is replaced atomically and keeps its permissions.
3. Create a new file with `fs_write` and `"create_only": true`; `"mkdirs": true` creates missing
   parent folders.

Replacing an existing file without `expect_sha256` returns
[`hash_required`](https://docs.cmdop.ai/reference/errors/hash_required.md) and writes nothing. When the file changed since
you read it, the write is refused: read it again, then decide.

## Remove a file

[`fs_delete`](https://docs.cmdop.ai/reference/tools/fs_delete.md) moves the file or folder to the user's trash, so the
owner can put it back; the answer says where (`trashed_to`). A folder that is not empty needs
`"recursive": true`. `"permanent": true` removes for good and cannot be undone: use it only where
the trash cannot take the file.

## What the file tools never reach

- **Keys and credentials**, for reading and writing: `~/.ssh`, `~/.gnupg`, `~/.aws`, `~/.azure`,
  `~/.kube`, `~/.docker`, `~/.netrc`, `~/.password-store`, `~/.git-credentials`,
  `~/Library/Keychains`, `~/.claude`, `~/.codex`, the credential folders under `~/.config` (`gh`,
  `gcloud`, `op`, `cmdop*`), and the machine program's own folder. `"secrets": true` in
  [`ceiling.json`](https://docs.cmdop.ai/reference/config/ceiling-json.md) lifts this list; it does not lift the next one.
- **Files no tool may change**, whatever the grant and the ceiling say: shell start files
  (`.bashrc`, `.bash_profile`, `.zshrc`, `.zprofile`, `.zshenv`, `.profile`), `.gitconfig`,
  `.claude.json`, `~/.ssh`, `~/.claude`, `~/.codex`, the keychains, any `.git` folder and
  `.mcp.json` file, `/etc`, and the machine program's own folder.
- **Folders outside the machine's roots**, when its ceiling lists `roots`.

A refusal returns [`denied`](https://docs.cmdop.ai/reference/errors/denied.md) with the reason. `exec_run` is not limited
by these folders: an agent with `exec` can do anything the user can.

## Check

`fs_stat` on the path shows the new `size` and `modified` time, and with `"hash": true` the same
`sha256` your write returned.

## Related

[Set the ceiling](https://docs.cmdop.ai/guides/access/ceiling.md) · [Risk classes](https://docs.cmdop.ai/concepts/risk-classes.md) ·
[Security model](https://docs.cmdop.ai/concepts/security-model.md)
