---
title: Watch and track files
description: List a sandbox's directories, watch files change as they happen, and ask what changed under a directory since a point in time.
---

Besides copying single files in and out (see [Commands and files](/docs/sandboxes/commands-and-files/)), pols can list a sandbox's directories, stream file changes as they happen and tell you what changed under a directory since you started tracking it. That answers "what did the agent touch?" without diffing the disk.

```sh
pols files ls web /home/user
pols files watch web /home/user/app          # until Ctrl-C
pols files track web /home/user/app
pols exec web -- bash -lc 'cd app && npm run codegen'
pols files changes web /home/user/app        # created, modified, deleted since track
pols files untrack web /home/user/app
```

These commands act with root's rights, or as a [sandbox user](/docs/reference/cli/users/) with `--user`, who sees only what that user may read. They need nothing installed in the sandbox and work on every sandbox, also ones made before they existed.

## Listing a directory

`pols files ls SANDBOX PATH` (`GET /v1/sandboxes/{sandbox}/files/list?path=`) lists a directory's entries with their type, size, permission bits and modification time, at most 10,000. Symbolic links are listed as links, not followed.

## Watching for changes

`pols files watch SANDBOX PATH` prints each file created, modified or deleted under a directory, and in the directories below it unless you pass `--no-recursive`, until you interrupt it. It can also watch a single file. New directories are watched as they appear. With `--json`, it prints the API's events, one JSON object per line:

```sh
pols files watch web /home/user/app --json | jq -r .path
```

Over the API, `GET /v1/sandboxes/{sandbox}/files/watch?path=` (with `recursive=false` for one level) streams NDJSON events until you close the connection:

| Event | Meaning |
| --- | --- |
| `ready` | the watch is in place; `path` is the watched path and `dir` says whether it is a directory |
| `created`, `modified`, `deleted` | a change to `path`, at `time` |
| `overflow` | some changes could not be reported; `message` says why |
| `error` | the watch ended, for example because the path went away or the sandbox stopped; always the last event |

A watch never drops changes silently. It watches at most 8,192 directories and reports at most one `modified` per file and second (a write it held back is reported when the file is closed or the second is over). What it cannot report, because there are too many directories, the sandbox's inotify limit is reached or the stream is read too slowly, comes as an `overflow` event.

An open watch counts as use of the sandbox, like an SSH session, so it keeps the sandbox from going to [standby](/docs/sandboxes/lifecycle/#standby), for at most 24 hours.

## Tracking what changed

When you only need the result, not a live stream, track a directory:

1. `pols files track SANDBOX DIR` (`POST .../files/changes?path=`) records what is under it now, at most 100,000 entries. Tracking the same directory again starts over.
2. `pols files changes SANDBOX DIR` (`GET .../files/changes?path=`) lists what was created, modified or deleted since, at most 10,000 entries, by path, each with its type and, unless it was deleted, its modification time.
3. `pols files untrack SANDBOX DIR` (`DELETE .../files/changes?path=`) forgets it.

Tracking is a comparison, not a log of events:

- The record is kept on the sandbox's disk, in the user's `~/.local/state/pols/changes`, so it stays across stop, resume and standby, and costs nothing while nobody asks. It is per user.
- A file that was changed and changed back counts as modified. A file created and deleted in between does not show.
- Directories are reported when they are created or deleted, not when their entries change.

## From agents and code

| | CLI | API | MCP | TypeScript and Python |
| --- | --- | --- | --- | --- |
| List | `pols files ls` | `GET .../files/list` | `file_list` | `files.list` |
| Watch | `pols files watch` | `GET .../files/watch` | none: a tool call cannot stream | `files.watch` |
| Track | `pols files track/changes/untrack` | `POST/GET/DELETE .../files/changes` | `file_changes` (`action` start, list or stop) | `files.track`, `.changes`, `.untrack` |

All of them take `user` to act as a sandbox user. A key [limited to one Linux user](/docs/account/key-limits/#keys-limited-to-one-linux-user) always acts as its user.

```python
pols.files.track("web", "/home/user/app")
# ... let the agent work ...
for change in pols.files.changes("web", "/home/user/app")["changes"]:
    print(change["kind"], change["path"])

for ev in pols.files.watch("web", "/home/user/app"):  # ready, then created/modified/deleted/overflow
    print(ev["type"], ev.get("path"))
```

To write several files in one call, tar them, copy the archive with `pols cp` and unpack it with `pols exec`. File changes are not recorded in the [timeline](/docs/sandboxes/timeline/).
