Files
TL;DR
The files service provides full file operations on remote machines β read, write, list, copy, move, delete, search, archive, and get file info. Supports reading files as strings or buffers, recursive directory listing with hidden files, pattern-based file search, and directory archiving. All operations target the machine set via setMachine().
Read, write, list, copy, move, and delete files on remote machines.
How do I set up the files service?
import { CMDOPClient } from '@cmdop/node';
// Connect via cloud relay and select the target machine for file operations
const client = await CMDOPClient.remote({ apiKey: 'cmdop_xxx' });
await client.files.setMachine('my-server');How do I list directory contents?
// List all files and directories at the given path
const files = await client.files.list('/var/log');
for (const file of files) {
console.log(file.name, file.size, file.isDirectory);
}With Options
// List with hidden files included, non-recursive (current directory only)
const files = await client.files.list('/home/user', {
includeHidden: true,
recursive: false,
});How do I read a file?
// Read a file as a UTF-8 string
const content = await client.files.read('/etc/nginx/nginx.conf');
console.log(content);Read as Buffer
// Read a binary file as a raw Buffer (e.g., images, executables)
const buffer = await client.files.readBuffer('/path/to/binary');How do I write a file?
// Write a Buffer to a file on the remote machine
await client.files.write('/tmp/config.json', Buffer.from('{"key": "value"}'));Write String
// Write a plain string directly -- auto-converted to UTF-8
await client.files.write('/tmp/hello.txt', 'Hello, World!');How do I copy a file?
// Copy a file from source path to destination path on the remote machine
await client.files.copy('/src/file.txt', '/dst/file.txt');How do I move or rename a file?
// Move (or rename) a file from old path to new path
await client.files.move('/old/path.txt', '/new/path.txt');How do I delete a file?
// Delete a single file on the remote machine
await client.files.delete('/tmp/old-file.txt');Recursive Delete
// Delete an entire directory and its contents recursively
await client.files.delete('/tmp/old-dir', { recursive: true });How do I create a directory?
// Create a new directory (creates parent directories if needed)
await client.files.mkdir('/new/dir');How do I search for files?
// Search for files matching a glob pattern within a directory tree
const results = await client.files.search('/var/log', {
pattern: '*.log', // Match all .log files
maxDepth: 3, // Search up to 3 levels deep
});How do I create an archive?
// Archive a directory into a compressed tar.gz file
await client.files.archive('/path/to/dir', '/tmp/backup.tar.gz');How do I get file metadata?
// Retrieve file size, modification time, and permission info
const info = await client.files.info('/etc/nginx/nginx.conf');
console.log(info.size, info.modified, info.permissions);What parameters do the file methods accept?
list(path, options?)
| Parameter | Type | Description |
|---|---|---|
path | string | Directory path |
options.includeHidden | boolean | Include hidden files (default: false) |
options.recursive | boolean | List recursively (default: false) |
read(path)
| Parameter | Type | Description |
|---|---|---|
path | string | File path to read |
write(path, content)
| Parameter | Type | Description |
|---|---|---|
path | string | File path to write |
content | string | Buffer | File content |
delete(path, options?)
| Parameter | Type | Description |
|---|---|---|
path | string | Path to delete |
options.recursive | boolean | Delete directories recursively (default: false) |
Last updated on