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

# Walks

> Answer find, du, ls -R and tree from one listing.

`find`, `du`, `ls -R`, `tree`, `grep -r` and `rg` can ask your VFS for a whole
folder tree in one call, instead of listing each folder. Return `None`
(TypeScript: `null`) and Mirage lists each folder.

For example, [S3](https://docs.mirage.strukto.ai/python/vfs/s3.md) answers `find /data/` from one recursive
listing of the prefix.

## `read_tree`

Every file and folder below one folder.

> [tab] Python
>
> ```python theme={null}
> async def read_tree(self, path: PathSpec) -> dict[str, FileStat] | None: ...
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> readTree(path: PathSpec): Promise<Map<string, FileStat> | null>
> ```

| Argument | Meaning |
| - | - |
| `path` | The folder. |

| Return | Mirage then |
| - | - |
| Every file and folder below `path`, keyed by its path below it (`guides`, `guides/start.md`), each with the `FileStat` that `stat` gives | Walks it without listing any folder. |
| `None` | Lists each folder with `readdir`. |

For a path that is not a folder, raise what `readdir` raises.

These commands call `read_tree`:

* `find`
* `du`
* `ls -R`
* `tree`
* `grep -r`, `rg`
* `cp -r` inside the mount, when your VFS overrides `copy`

These list each folder instead:

* `find -maxdepth N`
* `tree -L N`
* `rg --max-depth N`
* `ls` without `-R`
* `cp -r` to another mount

## `walk_limit`

Every walk stops at its command's
[`walk_limit`](https://docs.mirage.strukto.ai/home/integration/search#walk_limit) on your mount, whether
`read_tree` answered or Mirage listed each folder. It is a field of
[`command_limits`](https://docs.mirage.strukto.ai/home/core/output-limits): the `"@all"` entry sets it for every
command, and an entry named for one command, such as `find`, changes only how
far that command walks. The mount's `"@all"` entry, else the workspace's, also sets
your VFS's own `walk_limit`, which decides how it searches: with the service's
search under a cap, by reading every file at `-1`. Return `None` for a folder
too large to send in one call: Mirage then stops calling `readdir` once the
limit is spent.

To stop paging early, take `limit` (TypeScript: `handed.limit`), how many
entries the walk can still use:

> [tab] Python
>
> ```python theme={null}
> async def read_tree(self, path: PathSpec, limit: int | None = None) -> dict[str, FileStat] | None: ...
> async def readdir(self, path: PathSpec, limit: int | None = None) -> list[FileStat] | DirListing: ...
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> readTree(path: PathSpec, handed?: Handed): Promise<Map<string, FileStat> | null>
> readdir(path: PathSpec, handed?: Handed): Promise<readonly FileStat[] | DirListing>
> ```

| Function | Past `limit` entries |
| - | - |
| `read_tree` | Return `None`; Mirage lists each folder instead. |
| `readdir` | Return what you hold as a `DirListing` with `partial=True`; Mirage does not keep it as the whole folder. |

`limit` is `None` (TypeScript: `undefined`) when nothing caps the walk. Without
it, `read_tree` returns the whole tree and the command keeps what fits.

## Example

This example extends `NotesVFS` from [Custom VFS](https://docs.mirage.strukto.ai/home/setup/custom-vfs.md#example-a-read-only-vfs).
Its pages sit in one folder, so one `readdir` is the whole tree; a service with
nested folders would return them all from one recursive call:

> [tab] Python
>
> ```python theme={null}
> from mirage import FileStat, PathSpec
>
> from notes import NotesVFS
>
>
> class TreeNotesVFS(NotesVFS):
>     async def read_tree(self, path: PathSpec) -> dict[str, FileStat] | None:
>         return {entry.name: entry for entry in await self.readdir(path)}
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> import type { FileStat, PathSpec } from '@struktoai/mirage-node'
> import { NotesVFS } from './notes.ts'
>
> export class TreeNotesVFS extends NotesVFS {
>   override async readTree(path: PathSpec): Promise<Map<string, FileStat> | null> {
>     return new Map((await this.readdir(path)).map((entry) => [entry.name, entry]))
>   }
> }
> ```

`find /notes`, `du -a /notes`, `ls -R /notes` and `tree /notes` then call
`read_tree` once. `find /notes -maxdepth 1` lists the folder instead.


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