Skip to main content

Description

Lists all files and directories in the specified path. Supports recursive listing and filtering of hidden files.

Method Signature

async listFiles(
  path: string,
  options?: { recursive?: boolean; includeHidden?: boolean }
): Promise<ListFilesResult>

Parameters

path
string
required
Absolute path to the directory to list
options
object
Optional configuration for the listing operation
recursive
boolean
default:false
Whether to list files recursively in subdirectories
includeHidden
boolean
default:false
Whether to include hidden files (those starting with ’.’)

Returns

ListFilesResult
object
Result of the list operation
success
boolean
Whether the listing was successful
path
string
Path that was listed
files
FileInfo[]
Array of file and directory information
name
string
Name of the file or directory
absolutePath
string
Absolute path to the file or directory
relativePath
string
Path relative to the listed directory
type
'file' | 'directory' | 'symlink' | 'other'
Type of the entry
size
number
Size in bytes (0 for directories)
modifiedAt
string
ISO 8601 timestamp of last modification
mode
string
Unix file mode (e.g., ‘0644’)
permissions
object
File permissions
readable
boolean
Whether the file is readable
writable
boolean
Whether the file is writable
executable
boolean
Whether the file is executable
count
number
Total number of files and directories found
timestamp
string
ISO 8601 timestamp of when the operation completed
exitCode
number
Exit code from the underlying list command (0 = success)

Examples

List files in a directory

const result = await sandbox.listFiles('/workspace');
console.log(`Found ${result.count} items`);

for (const file of result.files) {
  console.log(`${file.name} (${file.type}) - ${file.size} bytes`);
}

Recursive listing

const result = await sandbox.listFiles('/workspace', {
  recursive: true
});

// Find all Python files
const pythonFiles = result.files.filter(
  file => file.type === 'file' && file.name.endsWith('.py')
);
console.log(`Found ${pythonFiles.length} Python files`);

Include hidden files

const result = await sandbox.listFiles('/workspace', {
  includeHidden: true
});

// Find .git directory
const gitDir = result.files.find(
  file => file.name === '.git' && file.type === 'directory'
);

Check file permissions

const result = await sandbox.listFiles('/workspace');

for (const file of result.files) {
  if (file.type === 'file') {
    console.log(`${file.name}:`);
    console.log(`  Readable: ${file.permissions.readable}`);
    console.log(`  Writable: ${file.permissions.writable}`);
    console.log(`  Executable: ${file.permissions.executable}`);
  }
}

Calculate directory size

const result = await sandbox.listFiles('/workspace', {
  recursive: true
});

const totalSize = result.files
  .filter(f => f.type === 'file')
  .reduce((sum, f) => sum + f.size, 0);

console.log(`Total size: ${totalSize} bytes`);

Error Handling

The method throws an error if:
  • The directory does not exist
  • The path is not a directory
  • Insufficient permissions to read the directory
try {
  const result = await sandbox.listFiles('/nonexistent');
} catch (error) {
  console.error('Failed to list files:', error.message);
}

Notes

  • Symbolic links are identified but not followed
  • The recursive option can be slow for large directory trees
  • Hidden files (starting with ’.’) are excluded by default

See Also

Build docs developers (and LLMs) love