Skip to main content
You can watch a directory for changes using the files.watchDir() method in JavaScript and files.watch_dir() method in Python.
Since events are tracked asynchronously, their delivery may be delayed. It’s recommended not to collect or close watcher immediately after making a change.
import { Sandbox, FilesystemEventType } from 'e2b'

const sandbox = await Sandbox.create()
const dirname = '/home/user'

// Start watching directory for changes
const handle = await sandbox.files.watchDir(dirname, async (event) => {
  console.log(event)
  if (event.type === FilesystemEventType.WRITE) {
    console.log(`wrote to file ${event.name}`)
  }
})

// Trigger file write event
await sandbox.files.write(`${dirname}/my-file`, 'hello')

Recursive watching

You can enable recursive watching using the parameter recursive.
When rapidly creating new folders (e.g., deeply nested path of folders), events other than CREATE might not be emitted. To avoid this behavior, create the required folder structure in advance.
import { Sandbox, FilesystemEventType } from 'e2b'

const sandbox = await Sandbox.create()
const dirname = '/home/user'

// Start watching directory for changes
const handle = await sandbox.files.watchDir(dirname, async (event) => {
  console.log(event)
  if (event.type === FilesystemEventType.WRITE) {
    console.log(`wrote to file ${event.name}`)
  }
}, {
  recursive: true
})

// Trigger file write event
await sandbox.files.write(`${dirname}/my-folder/my-file`, 'hello')

Including entry information

You can include information about the affected file or directory in each event using the parameter includeEntry in JavaScript and include_entry in Python. When enabled, each event carries the entry’s information — such as its path, type, and size — in the entry field.
Entry information may be unset for remove events since the path no longer exists. Including entry information requires templates with envd version v0.6.3 or above — using the includeEntry / include_entry option on older sandboxes will throw an error.
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create()
const dirname = '/home/user'

// Start watching directory for changes
const handle = await sandbox.files.watchDir(dirname, async (event) => {
  console.log(event.type, event.name, event.entry?.path, event.entry?.type)
}, {
  includeEntry: true
})

// Trigger file write event
await sandbox.files.write(`${dirname}/my-file`, 'hello')