Skip to main content
Glama

sed_multifile

Apply sed patterns to multiple files matching a glob pattern to make targeted text replacements across your codebase or documents.

Instructions

Apply sed pattern to multiple files matching a glob pattern

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
patternYesSed pattern to apply
filePatternYesFile glob pattern (e.g., "*.ts", "src/**/*.js")
directoryNoStarting directory for search.
backupNoCreate backup files

Implementation Reference

  • Handler implementation for sed_multifile tool. Locates files using 'find' with the given glob pattern, applies the sed pattern via 'perl -i -pe' to each matching file (with optional .bak backups), collects results, and returns a processing summary.
    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:67-94 (registration)
    Registration of the sed_multifile tool in the ListTools response, including name, description, and input schema definition.
    {
      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']
      }
    },
  • Input schema definition for sed_multifile tool, specifying parameters like pattern, filePattern, directory, and backup options.
    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']

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