List directory
import { Sandbox } from 'e2b'
const sandbox = await Sandbox.create({
template: 'base',
})
const dirContent = await sandbox.filesystem.list('/')
dirContent.forEach((item) => {
console.log(item.name)
})
await sandbox.close()
Create directory
Create directory and all parent directories.
import { Sandbox } from 'e2b'
const sandbox = await Sandbox.create({ template: 'base' })
// Create a new directory '/dir'
await sandbox.filesystem.makeDir('/dir')
await sandbox.close()
Write to file
When writing to a file that doesn't exist, the file will get created.
When writing to a file that already exists, the file will get overwritten.
When writing to a file that's in a directory that doesn't exist, you'll get an error.
import { Sandbox } from 'e2b'
const sandbox = await Sandbox.create({ template: 'base' })
// `filesystem.write()` will:
// - create the file if it doesn't exist
// - fail if any directory in the path doesn't exist
// - overwrite the file if it exists
// Write the content of the file '/hello.txt'
await sandbox.filesystem.write('/hello.txt', 'Hello World!')
// The following would fail because '/dir' doesn't exist
// await sandbox.filesystem.write("/dir/hello.txt", "Hello World!")
await sandbox.close()
Read from file
import { Sandbox } from 'e2b'
const sandbox = await Sandbox.create({ template: 'base' })
const fileContent = await sandbox.filesystem.read('/etc/hosts')
console.log(fileContent)
await sandbox.close()
Write bytes
import { Sandbox } from 'e2b'
const sandbox = await Sandbox.create({ template: 'base' })
// Let's convert string to bytes for testing purposes
const encoder = new TextEncoder('utf-8')
const contentInBytes = encoder.encode('Hello World!')
// `writeBytes` accepts a Uint8Array and saves it to a file inside the playground
await sandbox.filesystem.writeBytes('/file', contentInBytes)
// We can read the file back to verify the content
const fileContent = await sandbox.filesystem.read('/file')
// This will print 'Hello World!'
console.log(fileContent)
await sandbox.close()
Read bytes
import fs from 'fs'
import { Sandbox } from 'e2b'
const sandbox = await Sandbox.create({ template: 'base' })
// File bytes will read file's content as bytes
// `fileBytes` as a Uint8Array
const fileBytes = await sandbox.filesystem.readBytes('/etc/hosts')
// The output will look similar to this:
// <Buffer 31 32 37 2e 30 2e 30 2e 31 09 6c 6f 63 61 6c 68 6f 73 ...
console.log(fileBytes)
// We can save those bytes to a file locally like this:
fs.writeFileSync('hosts', fileBytes)
await sandbox.close()
Watch directory for changes
import { Sandbox } from 'e2b'
const sandbox = await Sandbox.create({ template: 'base' })
// Start filesystem watcher for the /home directory
const watcher = sandbox.filesystem.watchDir('/home')
watcher.addEventListener((event) => {
console.log('Filesystem event', event)
})
await watcher.start()
// Create files in the /home directory inside the playground
// We'll receive notifications for these events through the watcher we created above.
for (let i = 0; i < 10; i++) {
console.log('Creating file', i)
// `filesystem.write()` will trigger two events:
// 1. 'Create' when the file is created
// 2. 'Write' when the file is written to
await sandbox.filesystem.write(`/home/${i}.txt`, `Hello World ${i}!`)
}
await sandbox.close()