> ## Documentation Index
> Fetch the complete documentation index at: https://docs.styne.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Files

> Move files in and out of a sandbox, find and change them in place, and share a file with a link.

**You need** a running sandbox ([Quickstart](https://docs.styne.ai/quickstart.md)). The examples use the E2B SDK with `E2B_API_KEY` and `E2B_API_URL=https://api.styne.ai` set ([Switch from E2B](https://docs.styne.ai/e2b-drop-in.md)).

## Move files in and out

Four ways, from the smallest file to a whole folder. Pick by size and by who is moving the data.

| You want | Use |
| - | - |
| a small text or binary file from your code | the SDK: `files.write` and `files.read` |
| a file of any size, or a link for someone without a key | a [signed URL](https://docs.styne.ai/files.md#signed-upload-and-download-urls) |
| a whole folder, in or out | pack it with `tar` and move one file |
| files of a sandbox that is stopped or gone | read them from a [snapshot](https://docs.styne.ai/snapshots.md#look-inside-a-snapshot) |

### Small files with the SDK

```python Python theme={"theme":"css-variables"}
from e2b import Sandbox

with Sandbox.create(template="base", timeout=300) as sbx:
    sbx.files.write("/root/in/data.csv", "id,name\n1,ada\n2,grace\n")
    print(sbx.commands.run("wc -l /root/in/data.csv").stdout.strip())
    print(sbx.files.read("/root/in/data.csv"))
    print(",".join(f.name for f in sbx.files.list("/root/in")))
```

```typescript TypeScript theme={"theme":"css-variables"}
import { Sandbox } from 'e2b'

const sbx = await Sandbox.create('base', { timeoutMs: 300_000 })
try {
  await sbx.files.write('/root/in/data.csv', 'id,name\n1,ada\n2,grace\n')
  console.log((await sbx.commands.run('wc -l /root/in/data.csv')).stdout.trim())
  console.log(await sbx.files.read('/root/in/data.csv'))
  console.log((await sbx.files.list('/root/in')).map((f) => f.name).join(','))
} finally {
  await sbx.kill()
}
```

```bash CLI theme={"theme":"css-variables"}
printf 'id,name\n1,ada\n2,grace\n' > data.csv
styne sandbox files write "$SANDBOX_ID" /root/in/data.csv ./data.csv
styne sandbox files read "$SANDBOX_ID" /root/in/data.csv
```

The Python example prints:

```text theme={"theme":"css-variables"}
3 /root/in/data.csv
id,name
1,ada
2,grace

data.csv
```

`files.write` creates missing folders. Read binary files with `format="bytes"` (Python) or `{ format: 'bytes' }` (TypeScript).

### Any size with a signed URL

The API makes a link that carries its own signature, valid for one sandbox, one path and one direction until it expires.
Upload or download with plain `curl`, with no key and no SDK, streamed with no size cap besides the sandbox's disk.

```bash theme={"theme":"css-variables"}
curl -sS https://api.styne.ai/sandboxes/$SANDBOX_ID/files/signed-url \
  -H "Authorization: Bearer $STYNE_API_KEY" -H "content-type: application/json" \
  -d '{"path": "/root/out/result.tar", "operation": "download", "expiresIn": 600}'
```

The answer is `{"url", "method", "operation", "path", "expiresAt"}`. Fetch `url` with `method`:

```text theme={"theme":"css-variables"}
curl -o result.tar "<url from the answer>"
curl -X POST --data-binary @input.tar -H 'Content-Type: application/octet-stream' "<url of an upload link>"
```

### A whole folder with tar

```python theme={"theme":"css-variables"}
from e2b import Sandbox

with Sandbox.create(template="base", timeout=300) as sbx:
    sbx.files.write("/root/project/src/main.py", "print('hi')\n")
    sbx.files.write("/root/project/README.md", "# demo\n")
    sbx.commands.run("tar -C /root -czf /root/project.tgz project")
    blob = sbx.files.read("/root/project.tgz", format="bytes")
    print(len(blob) > 0)
    sbx.files.write("/root/restore/project.tgz", bytes(blob))
    sbx.commands.run("tar -C /root/restore -xzf /root/restore/project.tgz")
    print(sbx.commands.run("ls /root/restore/project").stdout.split())
```

```text theme={"theme":"css-variables"}
True
['README.md', 'src']
```

To sync a folder from your machine, create the archive locally (`tar -czf project.tgz project`), write it with
`files.write`, and extract it in the sandbox the way the last two lines do.

## Find and change files in place

Reading, writing, listing, moving and removing files works through the E2B SDKs. The routes below add what a coding agent
also needs: find files by name, find text in files, replace text across files, change permissions, and hand a short-lived
link to someone who has no API key. They are on the native API; the E2B SDKs do not have them. Every route is
`POST /sandboxes/{id}/files/...` with the account's API key, and answers JSON. Paths are absolute; a relative path starts in
`/root`, where a command starts when it names no `cwd`.

Every value you send reaches the sandbox as an argument, never as shell text, so a path or pattern with quotes, `$(...)`
or `;` in it is only data. Results are cut at a limit (default 1000, at most 10000; a `limit` outside 1 to 10000 is cut to that range) and say `"truncated": true` when there
was more.

### Search by name

```text theme={"theme":"css-variables"}
POST /sandboxes/{id}/files/search
{ "path": "/home/user/project", "pattern": "*.rs", "type": "file", "depth": 8, "limit": 200 }
```

`pattern` is a glob on the file name; a pattern with a `/` in it is matched against the whole path. `type` is `file`,
`directory` or `any` (default). `depth` is 1 to 64. The answer is `{"files": ["/home/user/project/src/a.rs", ...],
"truncated": false}`. `/proc`, `/sys` and `/dev` are never searched and symbolic links are not followed.

### Find text

```text theme={"theme":"css-variables"}
POST /sandboxes/{id}/files/find
{ "path": "/home/user/project", "query": "TODO", "ignoreCase": true, "glob": "*.rs", "limit": 100 }
```

`query` is a literal string; with `"regex": true` it is an extended regular expression (an invalid one is a 400).
`glob` limits the search to file names that match (a name pattern, no `/`). Binary files are skipped and so are files
over `maxFileKiB` (default 1024, at most 8192). The answer is `{"matches": [{"path", "line", "text"}], "truncated"}`;
a matching line is cut at 512 bytes.

### Replace text

```text theme={"theme":"css-variables"}
POST /sandboxes/{id}/files/replace
{ "path": "/home/user/project", "glob": "*.md", "old": "colour", "new": "color", "dryRun": true }
```

Replaces every occurrence of the literal text `old` by `new`. Name the files either with `files` (a list of at most
1000 paths) or with `path` and `glob`, which takes every text file under `path` that contains `old`. `dryRun` counts
without writing. The answer lists each changed file with its count: `{"files": [{"path", "replacements"}],
"replacements": 12, "truncated": false, "dryRun": true}`. A file that is not UTF-8 text or is over 8 MiB is listed with
an `error` and left alone. Each file is read and written back in place, so its mode and owner stay; there is no lock, so
a file the sandbox changes during the call is overwritten with the version this call read.

### Permissions

```text theme={"theme":"css-variables"}
POST /sandboxes/{id}/files/permissions
{ "path": "/home/user/run.sh", "mode": "755", "owner": "user", "group": "user", "recursive": false }
```

`mode` is 3 or 4 octal digits; `owner` and `group` are a user or group name, or a numeric id. Give at least one of the
three. The answer is the entry as it is afterwards: `{"entry": {"path", "mode", "permissions", "owner", "group", ...}}`.

## Signed upload and download URLs

```text theme={"theme":"css-variables"}
POST /sandboxes/{id}/files/signed-url
{ "path": "/home/user/big.tar", "operation": "download", "expiresIn": 600 }
```

`operation` is `upload` or `download`; `expiresIn` is 1 to 86400 seconds (default 300). The answer is
`{"url", "method", "operation", "path", "expiresAt"}`. The URL carries its own signature and needs no API key and no
access token:

```text theme={"theme":"css-variables"}
curl -o big.tar "https://49983-<id>.<domain>/files?path=%2Fhome%2Fuser%2Fbig.tar&signature=..."
curl -X POST --data-binary @big.tar -H 'Content-Type: application/octet-stream' "<upload url>"
```

A signature opens exactly one sandbox, one path and one operation until it expires: a download link cannot upload, and
a link for one path opens no other. An upload takes one file (an octet-stream body, or a multipart form with one file
part). Both directions are streamed through the API, not held in memory, so there is no size cap besides the sandbox's
disk. The links are made by this route only; the `signature` the E2B SDKs compute locally from the access token is not
checked here, and the SDKs' own calls keep using `X-Access-Token`.

## Things to know

* Files are on the sandbox's disk (1 GiB on `base`). They stay across a pause and go when the sandbox is killed: take
  what you need out first.
* After a kill, a [snapshot](https://docs.styne.ai/snapshots.md) is the only copy of the disk; run `sync` before you save it so every file is
  on it.
* On the routes of the native API a relative path starts in `/root`, the folder a command starts in. The E2B SDKs resolve
  relative paths in `/home/user`, as E2B does.

## Operations

| Method | Path | What it does |
| - | - | - |
| `GET` | `/sandboxes/{id}/files` | Read a file: the bytes of the file at the path query |
| `PUT` | `/sandboxes/{id}/files` | Write a file: the request body is the content; folders on the way are created |
| `DELETE` | `/sandboxes/{id}/files` | Delete a file or folder; answers 204 |
| `GET` | `/sandboxes/{id}/files/list` | List a directory |
| `POST` | `/sandboxes/{id}/files/search` | Find files by name under a path |
| `POST` | `/sandboxes/{id}/files/find` | Find text in the files under a path |
| `POST` | `/sandboxes/{id}/files/replace` | Replace text across files |
| `POST` | `/sandboxes/{id}/files/permissions` | Set mode, owner and group of a path |
| `POST` | `/sandboxes/{id}/files/signed-url` | Make a short-lived upload or download URL for one file |


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