Skip to main content
Glama

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
NameRequiredDescriptionDefault
backupNoCreate backup files
directoryNoStarting directory for search.
filePatternYesFile glob pattern (e.g., "*.ts", "src/**/*.js")
patternYesSed pattern to apply

Implementation Reference

  • 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')}` }] }; }
  • 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'] } },

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/MikeyBeez/mcp-smalledit'

If you have feedback or need assistance with the MCP directory API, please join our Discord server