What you will be able to do
- Choose Grep for searching file contents and Glob for matching file paths, and explain why each is the wrong tool for the other's job
- Recover from a failed Edit by lengthening old_string, by setting replace_all when every match should change, or by using Read + Write when no unique anchor exists
- Explore an unfamiliar codebase step by step: search for an entry point, then read along the call chain
- Trace a function through a wrapper module by listing its exported names and searching for each one
Key concept
Content search vs path search — Grep looks inside files for text such as a function name, an error string or an import. Glob looks only at file names and paths, such as every file matching **/*.test.tsx. Picking the right built-in tool starts with knowing which of the two questions you are asking.
1.Grep searches contents, Glob matches paths
Claude Code comes with a small set of built-in tools for working with a project's files, and each one answers a different kind of question. Read loads a file's contents. Write creates a file or replaces one completely. Edit changes a targeted span of text inside an existing file. Bash runs any shell command. Grep and Glob both search, but they search different things, and that difference is the one to learn first.
| Tool | Description | Permission required |
|---|---|---|
| Read | Reads the contents of files | No |
| Grep | Searches for patterns in file contents | No |
| Glob | Finds files based on pattern matching | No |
| Edit | Makes targeted edits to specific files | Yes |
| Write | Creates or overwrites files | Yes |
| Bash | Executes shell commands in your environment | Yes |
Grep looks inside files. Use it when the thing you want is a string in the code: every caller of validateToken, the file that raises "session expired", every module that imports authClient. The search pattern is a regular expression, so a single search covers validateToken( everywhere it is called. The result tells you where that text lives, which is what you need to find callers, error sources and dependencies.
Glob looks only at names. Use it when what you know is a naming convention: **/*.test.tsx for every React test file, or src/components/**/*.tsx for every component. Glob never opens a file. That makes it the right tool for structural questions like "which components have no test file?" when tests follow a fixed naming pattern. Glob the components, Glob the tests, and compare the two lists of paths. Searching file contents for the word test answers a different question: it misses tests that live in separate files, and it matches comments that mention testing.
Both are better choices than doing the same search through Bash with grep or find. Grep, Glob and Read need no permission because they only observe. Bash can run anything, so every Bash call needs permission, even a harmless search. That is why the read-only analysis set for an Agent SDK agent is exactly Read, Glob and Grep, with Bash added only when the agent has to execute something. Which tools each agent gets is covered in distributing tools across agents.
| Tools | What the agent can do |
|---|---|
| Read, Glob, Grep | Read-only analysis |
| Read, Edit, Glob | Analyze and modify code |
| Read, Edit, Bash, Glob, Grep | Full automation |
Grep for the calls, because the call is text inside files. Glob for the stories, because *.stories.tsx is a file-name pattern and no file needs to be opened.
Checkpoint 1 of 6· Check yourself
Your project's tests always sit in their own files named *.test.tsx. You need to know which React components under src/components have no test file yet. Which approach answers this?
Whether a test file exists depends on file paths, not file contents, and the tests follow a naming pattern. That makes it a job for Glob: list the components, list the tests, and compare the two lists. Grepping the components for "test" misses tests kept in separate files and matches comments that mention testing.
“Glob | Finds files based on pattern matching.”Source: code.claude.com
Checkpoint 2 of 6· Exam question
An architect asks Claude Code to count how many files in the src/components directory tree currently have no matching test file at all, as a first step in planning test coverage work. Assume that the project follows a consistent and reliable naming convention that pairs each component file with its corresponding test file (for example, Button.tsx is tested by Button.test.tsx). Which approach most directly answers this without unnecessary file reads?
Correct answer: A — Use Glob to list component files and test files separately by their naming pattern, then compare the two path lists for gaps
- A. Correct. Because the question explicitly states a consistent and reliable naming convention, glob patterns can match file paths by name without reading file contents. Listing component files (e.g.,
src/components/**/*.tsxexcluding test files) and test files (e.g.,src/components/**/*.test.tsx) and comparing the two lists directly reveals components lacking a corresponding test file. This static approach avoids unnecessary file reads and is the most direct method for this task. Note: If the naming convention were not reliable, a coverage tool would be more appropriate. - B. Incorrect. Running the full test suite executes code and measures assertion or coverage behavior, not the presence or absence of a matching test file. A component may have a test file that is skipped, broken, or otherwise reports zero assertions, leading to false positives. This approach is also more expensive and indirect than a static filename comparison.
- C. Incorrect. Reading every component file is precisely the unnecessary file-read cost the task seeks to avoid, and manual inspection is error-prone. A
describeblock is an implementation detail of test code; looking inside source files does not reliably establish whether a separate test file exists. - D. Incorrect. Grepping component source for the string "test" reads file contents and checks an irrelevant signal: a component may mention "test" in comments or code without having a test file, or have a test file without containing that word. It does not directly determine matching filenames.
2.Edit needs a unique anchor; Read + Write is the fallback
Edit replaces text it finds by exact match. You give it an old_string and a new string, and it swaps one for the other. It has no line numbers and no function names, only that string. Two rules decide whether the call works. The first is matching: old_string must appear in the file exactly as written, and one character of different whitespace or indentation is enough to miss. The second is uniqueness: old_string must appear exactly once. Edit also expects Claude to have read the file earlier in the same conversation, so Read normally comes before Edit.
A duplicate match has two correct fixes, and the intent decides which one. If only one occurrence should change, make old_string longer: include the function signature or the line above, so the combined text appears once in the file. If every occurrence should change, set replace_all: true. That flag replaces all of them, never just the first. Searching with Grep first doesn't help with this. Grep can show you where the duplicates are, but Edit picks its target from old_string alone, so retrying the same string fails the same way.
When no unique anchor can be built, use Read, then Write. Some files repeat the same text so many times, such as generated config, repeated blocks or identical boilerplate, that no amount of context makes a string appear only once. In that case, Read the whole file, make the change in the full contents, and Write the complete result back. Write creates or overwrites a file. It does not patch it. Pass it a single line and the file becomes that single line. This is why Write is the fallback and not the first choice: it rewrites the whole file, while Edit touches only the matched span.
| Situation | Correct move |
|---|---|
| old_string differs by whitespace or indentation | Copy the exact text from a fresh Read and retry Edit |
| old_string matches several times, only one should change | Extend old_string with surrounding context and retry Edit |
| old_string matches several times, all should change | Retry Edit with replace_all: true |
| No unique anchor exists at all | Read the full file, then Write the complete new contents |
Checkpoint 3 of 6· Check yourself
A generated config file repeats the same block so many times that adding surrounding context never makes any old_string match only once. Only one copy of the block should change. Which recovery works?
When no unique anchor can be built, the fallback is Read followed by Write. Write creates or overwrites the whole file, so it needs the complete new contents; given a single block, the file becomes just that block. replace_all changes every occurrence, and Edit chooses its target only from old_string, so line numbers from Grep don't help.
“Write | Creates or overwrites files.”Source: code.claude.com
Checkpoint 4 of 6· Exam question
A developer wants Claude Code to update a deprecated log statement logger.warn("legacy-path") that appears twice in the same file, in two different functions, where only one of the two occurrences should change. Claude issues an Edit call with old_string set to exactly that log statement and the call fails. What is the correct next step?
Correct answer: B — Extend old_string with the neighbouring lines from the target function, then retry Edit, which finds a single match in the file
- A. Incorrect. Write replaces the whole file with the content it is given and has no way to target one line. Passing a single log line would discard the rest of the file instead of changing the right function.
- B. Correct. Edit only applies when old_string matches exactly one place in the file. Adding the neighbouring lines that appear only in the target function makes the string unique, so the retry succeeds and the other function is untouched.
- C. Incorrect. replace_all rewrites every match of old_string, not just the first one. Both log statements would change, including the one the developer wants to keep.
- D. Incorrect. Grep is a read-only search and its output does not influence Edit. Edit matches on the old_string text alone, so the same string still matches twice and the retry fails the same way.
Sources1
3.Exploring a codebase: search first, then read
Understanding builds up one hop at a time, not in one bulk load. Claude Code's own working loop shows the order: search for the relevant source files, read those files, then act. Reading every file under src/ before you know where the login flow starts fills the context window with code that has nothing to do with the task. Picking files by size or by recent modification date is guessing, and it has the same cost. Why that context matters over a long exploration is covered in context management in large codebases.
Take a concrete task: trace a login request from its HTTP route handler down to the database call, in a repository Claude has never seen.
- Read CLAUDE.md, or AGENTS.md if the repo has one, for architecture and conventions. That is the purpose of those files (CLAUDE.md hierarchy).
- Grep for an entry point, such as the login route path or the handler's name.
- Read the handler file, and note what it imports.
- Read the next module along the call chain. Grep again whenever a name's definition isn't obvious.
- Stop when you reach the database call. Files outside the chain are never loaded.
Wrapper modules break a simple search, so list their exports first. A module that wraps a library or an internal service often re-exposes its functionality under its own names. Grepping for the underlying function finds only the wrapper and misses the code that calls the wrapper. The reliable procedure is in two steps. First, Read the wrapper and write down every name it exports. Then Grep for each exported name across the codebase. The combined results are the real set of call sites, including callers that never mention the original function.
Checkpoint 5 of 6· Check yourself
Claude has to trace how a password-reset request goes from its HTTP route to the database write, in a repository it has never seen. After reading CLAUDE.md, what should it do next?
Exploration is incremental. Claude searches for the relevant entry point first, then reads only the files along the call chain, following imports until it reaches the database call. Reading everything up front fills the context with unrelated code, and choosing files by size or modification date is guessing.
“Search for the relevant source files”Source: code.claude.com
Checkpoint 6 of 6· Exam question
A newly onboarded architect asks Claude Code to trace how a login request flows from the HTTP route handler through to the database call, in a codebase Claude has not explored yet. To build this understanding efficiently while keeping context usage low, what is the best incremental strategy?
Correct answer: A — Start by reading CLAUDE.md or AGENTS.md if they exist to gain high-level architecture context, then use Grep to locate the route handler and its imports, and read files incrementally along the call chain.
- A. According to Anthropic's documentation,
CLAUDE.mdandAGENTS.mdare project context files that Claude Code reads at the start of every session, providing persistent high-level context without manual prompting. Leveraging these files first gives the architect necessary architectural overview, then usinggrepto pinpoint the route handler and progressively reading only relevant files keeps context usage low and targets the specific call chain efficiently. This incremental approach avoids overwhelming context with irrelevant files and aligns with recommended onboarding practices. - B. Reading every file upfront is extremely inefficient and wastes context window. Claude Code has limited context capacity, and loading entire codebases leads to unnecessary token consumption and slower performance. The goal is to trace a specific flow, not understand the entire codebase at once.
- C. File size does not reliably indicate where the login flow logic resides. This heuristic is arbitrary and likely includes many irrelevant large files (e.g., config, test fixtures, generated code). It wastes context and misses the targeted incremental exploration needed.
- D. Recent file modification does not guarantee relevance to the login flow. Other unrelated features might have been updated recently. This approach ignores project structure and the specific call chain, making it inefficient and prone to missing the actual login-related files.
Sources
(3)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.https://code.claude.com/docs/en/tools-referenceOfficial docs
“Searches for patterns in file contents.”
↩︎ Grep searches contents, Glob matches paths“Finds files based on pattern matching.”
↩︎ Grep searches contents, Glob matches paths“Executes shell commands in your environment. See Bash tool behavior | Yes”
↩︎ Grep searches contents, Glob matches paths“Match: old_string must appear in the file exactly as written. A single character of whitespace or indentation difference is enough to miss.”
↩︎ Edit needs a unique anchor; Read + Write is the fallback“Uniqueness: old_string must appear exactly once.”
↩︎ Edit needs a unique anchor; Read + Write is the fallback“Claude reads the file in the current conversation before editing it”
↩︎ Edit needs a unique anchor; Read + Write is the fallback“Creates or overwrites files.”
↩︎ Edit needs a unique anchor; Read + Write is the fallback“Searches for patterns in file contents.”
↩︎ Exploring a codebase: search first, then read“Searches for patterns in file contents.”
↩︎ Key concept“Finds files based on pattern matching.”
↩︎ Exam trap 1“or sets replace_all: true to replace them all”
↩︎ Exam trap 2“Creates or overwrites files.”
↩︎ Exam trap 3“Glob | Finds files based on pattern matching.”
↩︎ Checkpoint“Claude either supplies a longer string with enough surrounding context to pin down one occurrence”
↩︎ Prediction“Write | Creates or overwrites files.”
↩︎ Checkpoint - 2.
“Find files by pattern, search content with regex, explore codebases”
↩︎ Grep searches contents, Glob matches paths“Read those files to understand the code”
↩︎ Exploring a codebase: search first, then read“A markdown file where you store project-specific instructions, conventions, and context that Claude should know every session.”
↩︎ Exploring a codebase: search first, then read“Search for the relevant source files”
↩︎ Exam trap 4“Search for the relevant source files”
↩︎ Checkpoint - 3.https://code.claude.com/docs/en/common-workflowsOfficial docs
“trace the login process from front-end to database”
↩︎ Exploring a codebase: search first, then read