sed_multifile
Apply sed patterns to multiple files matching a glob pattern in a specified directory. Create backups to ensure changes are secure and reversible.
Instructions
Apply sed pattern to multiple files matching a glob pattern
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| backup | No | Create backup files | |
| directory | No | Starting directory for search | . |
| filePattern | Yes | File glob pattern (e.g., "*.ts", "src/**/*.js") | |
| pattern | Yes | Sed pattern to apply |
Implementation Reference
- src/index.ts:608-644 (handler)The main execution logic for the 'sed_multifile' tool. It uses 'find' to locate files matching the glob pattern, applies the sed/perl substitution pattern to each file using 'perl -i', handles backups, and returns a summary of processed files with success/error indicators.case 'sed_multifile': { const { pattern, filePattern, directory = '.', backup = true } = args; // Use find to get files matching pattern const findCmd = `find ${directory} -name "${filePattern}" -type f`; const { stdout: files } = await execAsync(findCmd); if (!files.trim()) { return { content: [{ type: 'text', text: `No files found matching pattern: ${filePattern}` }] }; } const fileList = files.trim().split('\n'); const results = []; for (const file of fileList) { try { const backupExt = backup ? '.bak' : ''; const sedCmd = `perl -i${backupExt} -pe '${pattern}' '${file}'`; await execAsync(sedCmd); results.push(`✓ ${file}`); } catch (error) { results.push(`✗ ${file}: ${error.message}`); } } return { content: [{ type: 'text', text: `Processed ${fileList.length} files:\n${results.join('\n')}` }] }; }
- src/index.ts:70-92 (schema)JSON Schema defining the input parameters for the sed_multifile tool, including required fields 'pattern' and 'filePattern', optional 'directory' and 'backup'.inputSchema: { type: 'object', properties: { pattern: { type: 'string', description: 'Sed pattern to apply' }, filePattern: { type: 'string', description: 'File glob pattern (e.g., "*.ts", "src/**/*.js")' }, directory: { type: 'string', default: '.', description: 'Starting directory for search' }, backup: { type: 'boolean', default: true, description: 'Create backup files' } }, required: ['pattern', 'filePattern']
- src/index.ts:67-94 (registration)Registration of the sed_multifile tool in the ListToolsRequestSchema handler's tools array, including name, description, and full input schema.{ name: 'sed_multifile', description: 'Apply sed pattern to multiple files matching a glob pattern', inputSchema: { type: 'object', properties: { pattern: { type: 'string', description: 'Sed pattern to apply' }, filePattern: { type: 'string', description: 'File glob pattern (e.g., "*.ts", "src/**/*.js")' }, directory: { type: 'string', default: '.', description: 'Starting directory for search' }, backup: { type: 'boolean', default: true, description: 'Create backup files' } }, required: ['pattern', 'filePattern'] } },