list_backups
Find and display backup files in a specified directory using a custom file pattern. Streamlines file management and ensures quick access to relevant backups.
Instructions
List all backup files in a directory
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| directory | No | Directory to search for backup files | . |
| pattern | No | Backup file pattern | *.bak |
Implementation Reference
- src/index.ts:859-900 (handler)The handler function for the 'list_backups' tool. It uses the 'find' command to locate backup files matching the given pattern in the specified directory (default '.'), limits to 100 files, then for each retrieves modification time and size using 'stat' (cross-platform), computes formatted info including inferred original filename by removing common backup extensions, and returns a text response listing them with a tip.case 'list_backups': { const { directory = '.', pattern = '*.bak' } = args; // Find all backup files const findCmd = `find ${directory} -name "${pattern}" -type f | head -100`; const { stdout } = await execAsync(findCmd); if (!stdout.trim()) { return { content: [{ type: 'text', text: `No backup files found matching pattern: ${pattern}` }] }; } const files = stdout.trim().split('\n'); // Get file info for each backup const backupInfo = []; for (const backupFile of files) { try { const stats = await execAsync(`stat -f "%m %z" '${backupFile}' 2>/dev/null || stat -c "%Y %s" '${backupFile}'`); const [mtime, size] = stats.stdout.trim().split(' '); const date = new Date(parseInt(mtime) * 1000).toISOString().split('T')[0]; const sizeKB = Math.round(parseInt(size) / 1024); // Infer original file name const originalFile = backupFile.replace(/\.(bak|backup|orig|~)$/, ''); backupInfo.push(`${backupFile} (${sizeKB}KB, ${date}) -> ${originalFile}`); } catch { backupInfo.push(backupFile); } } return { content: [{ type: 'text', text: `Found ${files.length} backup files:\n\n${backupInfo.join('\n')}\n\nTip: Use restore_backup to restore any of these files` }] }; }
- src/index.ts:246-264 (registration)The registration of the 'list_backups' tool in the ListToolsRequestSchema handler, including name, description, and full input schema definition.{ name: 'list_backups', description: 'List all backup files in a directory', inputSchema: { type: 'object', properties: { directory: { type: 'string', default: '.', description: 'Directory to search for backup files' }, pattern: { type: 'string', default: '*.bak', description: 'Backup file pattern' } } } },
- src/index.ts:249-262 (schema)Input schema definition for the list_backups tool, specifying optional directory (default '.') and pattern (default '*.bak') parameters.inputSchema: { type: 'object', properties: { directory: { type: 'string', default: '.', description: 'Directory to search for backup files' }, pattern: { type: 'string', default: '*.bak', description: 'Backup file pattern' } }
- src/index.ts:533-558 (helper)Help content string for the list_backups tool, providing description, usage examples, and output format details.list_backups: `list_backups - Find backup files ============================== List all backup files in a directory. Examples: // List all .bak files in current directory list_backups({}) // Search specific directory list_backups({ directory: "./src" }) // Find different backup patterns list_backups({ pattern: "*.backup" }) list_backups({ pattern: "*~" }) // Emacs-style // Check entire project list_backups({ directory: ".", pattern: "*.bak" }) Output shows: - Backup file path - File size - Modification date - Original file name (inferred) Useful for cleanup or finding old versions. `