ailiteracynepal 🇳🇵
Text size

Chapter 03 · Section III · 12 min read

Bash and search: Grep, Glob

For anything the file tools cannot express, Claude Code reaches for the shell. Understanding what runs, where, and how it composes with the search tools is what makes multi-step tasks fast instead of frustrating.

The file tools are for changing files. Bash is for everything else. Running tests, invoking Git, calling out to curl, kicking off a build, starting a background process — all of it lives here. And because the shell is powerful, it is also the tool that most rewards understanding.

Bash

Bash runs a shell command in the session’s working directory. A few things are worth knowing.

  • Output comes back into the conversation. If it is huge, Claude will summarise; you can always ask to see the raw log.
  • Working directory is fixed. Do not fight it by writing cd /somewhere && command — Claude generally uses absolute paths instead. Cleaner and safer.
  • Long-running commands can go to the background. For a dev server or a build, Claude can pass run_in_background: true and get notified when it exits.
  • The command is shown to you before it runs. Read it. rm -rf .git looks the same as rm -rf dist at a glance if you are not paying attention.

Grep

Grep searches for regex patterns across files. Think of it as git grep with a few conveniences bolted on:

  • Regex, not literal. Escape special characters if you mean them literally.
  • Filters by file type. type: "ts" narrows to TypeScript files.
  • Multiple output modes. Full file paths, file paths with matching lines, count-only.

Reach for Grep when you know what string to look for. “Where is parseInvoice defined?” is a perfect Grep question.

Glob

Glob finds files by path pattern. src/**/*.tsx, docs/*.md, tests/**/*.test.ts. It complements Grep: Grep finds content, Glob finds locations.

Reach for Glob when you know where files should be but not what they contain. “Are there any .env.example files anywhere in the repo?” is a perfect Glob question.

Composing them

The typical multi-step search is Glob → Grep → Read.

  • Glob to enumerate candidate files.
  • Grep to narrow to the ones that mention the concept.
  • Read to actually pull the file into the conversation.

Or, when the goal is a fix:

  • Grep to find every call site of the old function.
  • Edit (with replace_all if you have already sanity-checked uniqueness) or a series of surgical Edits to update them.
  • Bash to run the tests and confirm nothing broke.

When Bash is the wrong answer

  • Editing a file. Prefer Edit over sed -i. Same result, safer.
  • Reading a file. Prefer Read over cat. Same result, better formatting.
  • Listing a directory. Prefer Glob over ls if you want structured results.

Bash for the shell things only shell can do; the dedicated tools for the rest.

Check your understanding

Quick check

You want to find every file in the repo that imports the deprecated `oldClient` module and update its import to `newClient`. Which sequence is best?