Current Working Directory

You can set a working directory either for the whole sandbox, a filesystem operation, or a new process.

Sandbox

If the current working directory for the sandbox is not set, it will default to the home directory - /home/user.

import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create({
  template: 'base',
  cwd: '/code', 
})

// You can also change the cwd of an existing sandbox
sandbox.cwd = '/home' 

await sandbox.close()

Filesystem

All filesystem operations with relative paths are relative to the current working directory.

import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create({
  template: 'base',
  cwd: '/home/user/code', 
})

await sandbox.filesystem.write('hello.txt', 'Welcome to E2B!') 
const proc = await sandbox.process.start({
  cmd: 'cat /home/user/code/hello.txt',
})
await proc.wait()
console.log(proc.output.stdout)
// output: "Welcome to E2B!"

await sandbox.filesystem.write('../hello.txt', 'We hope you have a great day!') 
const proc2 = await sandbox.process.start({cmd: 'cat /home/user/hello.txt'})
await proc2.wait()
console.log(proc2.output.stdout)
// output: "We hope you have a great day!"

await sandbox.close()

Process

If you set a working directory for the sandbox, all processes will inherit it. You can override it for each process.

import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create({
  template: 'base',
  cwd: '/code', 
})

const sandboxCwd = await sandbox.process.start({cmd: 'pwd'}) 
await sandboxCwd.wait()
console.log(sandboxCwd.output.stdout)
// output: /code

const processCwd = await sandbox.process.start({cmd: 'pwd', cwd: '/home'}) 
await processCwd.wait()
console.log(processCwd.output.stdout)
// output: /home

await sandbox.close()