File System Operations
The Daytona SDK provides comprehensive file system operations through the fs
module in Sandboxes. This guide covers all available file system operations and best practices.
Basic Operations
Daytona SDK provides an option to interact with the file system in Sandboxes. You can perform various operations like listing files, creating directories, reading and writing files, and more.
Listing Files and Directories
Daytona SDK provides an option to list files and directories in a Sandbox using Python and TypeScript.
# List files in a directoryfiles = sandbox.fs.list_files("/workspace")
for file in files: print(f"Name: {file.name}") print(f"Is directory: {file.is_dir}") print(f"Size: {file.size}") print(f"Modified: {file.mod_time}")
// List files in a directoryconst files = await sandbox.fs.listFiles("/workspace")
files.forEach(file => { console.log(`Name: ${file.name}`) console.log(`Is directory: ${file.isDir}`) console.log(`Size: ${file.size}`) console.log(`Modified: ${file.modTime}`)})
Creating Directories
Daytona SDK provides an option to create directories with specific permissions using Python and TypeScript.
# Create a directorysandbox.fs.create_folder("/workspace/new-dir")
# Create with specific permissions
sandbox.fs.create_folder("/workspace/new-dir", "755")
// Create a directoryawait sandbox.fs.createFolder("/workspace/new-dir")
// Create with specific permissionsawait sandbox.fs.createFolder("/workspace/new-dir", "755")
Uploading Files
Daytona SDK provides options to read, write, upload, download, and delete files in Sandboxes using Python and TypeScript.
Uploading a Single File
If you want to upload a single file, you can do it as follows:
root_dir = sandbox.get_user_root_dir()
# Upload a single file
with open("local_file.txt", "rb") as f: content = f.read()sandbox.fs.upload_file(root_dir + "/remote_file.txt", content)
const rootDir = await workspace.getUserRootDir()
// Upload a single fileconst fileContent = new File( [Buffer.from('Hello, World!')], 'data.txt', { type: 'text/plain' })await workspace.fs.uploadFile(rootDir + "/data.txt", fileContent)
Uploading Multiple Files
The following example shows how to efficiently upload multiple files with a single method call.
root_dir = sandbox.get_user_root_dir()
# Upload multiple files at once
files_to_upload = []
with open("file1.txt", "rb") as f1: files_to_upload.append(FileUpload( path=root_dir + "/data/file1.txt", content=f1.read() ))
with open("file2.txt", "rb") as f2: files_to_upload.append(FileUpload( path=root_dir + "/data/file1.txt", content=f2.read() ))
with open("settings.json", "rb") as f3: files_to_upload.append(FileUpload( path=root_dir + "/config/settings.json", content=f3.read() ))
sandbox.fs.upload_files(files_to_upload)
const rootDir = await workspace.getUserRootDir()
// Upload multiple files at onceconst files = [ { path: rootDir + '/data/file1.txt', content: new File(['Content of file 1'], 'file1.txt') }, { path: rootDir + '/data/file2.txt', content: new File(['Content of file 2'], 'file2.txt') }, { path: rootDir + '/config/settings.json', content: new File(['{"key": "value"}'], 'settings.json') }]
await workspace.fs.uploadFiles(files)
Downloading Files
The following commands downloads the file file1.txt
from the Sandbox user’s root directory and prints out the content:
root_dir = sandbox.get_user_root_dir()
content = sandbox.fs.download_file(root_dir + "/file1.txt")
with open("local_file.txt", "wb") as f: f.write(content)
print(content.decode('utf-8'))
const rootDir = await workspace.getUserRootDir()
const downloadedFile = await sandbox.fs.downloadFile(rootDir + "/file1.txt")
console.log('File content:', downloadedFile.toString())
Deleting files
Once you no longer need them, simply delete files by using the delete_file
function.
sandbox.fs.delete_file("/workspace/file.txt")
await sandbox.fs.deleteFile("/workspace/file.txt")
Advanced Operations
Daytona SDK provides advanced file system operations like file permissions, search and replace, and more.
File Permissions
Daytona SDK provides an option to set file permissions, get file permissions, and set directory permissions recursively using Python and TypeScript.
# Set file permissionssandbox.fs.set_file_permissions("/workspace/file.txt", "644")
# Get file permissions
file_info = sandbox.fs.get_file_info("/workspace/file.txt")print(f"Permissions: {file_info.permissions}")
// Set file permissionsawait sandbox.fs.setFilePermissions("/workspace/file.txt", { mode: "644" })
// Get file permissionsconst fileInfo = await sandbox.fs.getFileDetails("/workspace/file.txt")console.log(`Permissions: ${fileInfo.permissions}`)
File Search and Replace
Daytona SDK provides an option to search for text in files and replace text in files using Python and TypeScript.
# Search for text in files; if a folder is specified, the search is recursiveresults = sandbox.fs.find_files( path="/workspace/src", pattern="text-of-interest")for match in results: print(f"Absolute file path: {match.file}") print(f"Line number: {match.line}") print(f"Line content: {match.content}") print("\n")
# Replace text in files
sandbox.fs.replace_in_files( files=["/workspace/file1.txt", "/workspace/file2.txt"], pattern="old_text", new_value="new_text")
// Search for text in files; if a folder is specified, the search is recursiveconst results = await sandbox.fs.findFiles({ path="/workspace/src", pattern: "text-of-interest"})results.forEach(match => { console.log('Absolute file path:', match.file) console.log('Line number:', match.line) console.log('Line content:', match.content)})
// Replace text in filesawait sandbox.fs.replaceInFiles( ["/workspace/file1.txt", "/workspace/file2.txt"], "old_text", "new_text")