Searching with Grep
Module: Core Features | Lesson: 6 of 7 | Time: ~10 minutes
What You Will Learn
- How the Grep tool searches inside files
- Basic regex patterns for common searches
- When to use Grep vs Glob
Prerequisites
- Completed Searching with Glob
Why This Matters
In the last lesson, you learned how Glob finds files by name. But what if you know what is inside a file, not what it is called? Maybe you remember writing the word "TODO" somewhere but cannot recall which file it was in. Or you need to find every file that mentions a specific function name. The Grep tool searches inside files for text you specify -- it is like using Ctrl+F, but across your entire project at once.
How Grep Works
Grep opens every file (or a filtered set of files) and scans the text inside them. When it finds a line that matches your search, it reports:
- Which file contains the match
- Which line number the match is on
- The matching text itself
Here is what it looks like when Claude uses Grep:
─ Grep: "TODO"
src/app.js:12: // TODO: add error handling
src/utils.js:5: // TODO: refactor this function
docs/guide.md:34: - TODO: write the installation section
Each result shows filename:line-number: matching line. In this example, "TODO" appears in three files on three different lines.
Asking Claude to Search File Contents
You can ask in plain English:
> Search all files for the word "password"
> Which files mention "calculateTotal"?
> Find every TODO comment in this project
> Where is the function called "handleClick" defined?
> Search for "database" in the config files
Claude will use the Grep tool and show you every match.
Basic Regex: Flexible Searching
Grep supports something called regex (short for "regular expressions"). Do not let the name scare you -- at its core, regex is just a way to search for flexible patterns instead of exact text.
Here are the only regex features you need to know as a beginner:
Exact Text (Default)
Just type what you are looking for. No special characters needed:
"hello" -- finds the exact text "hello"
"error" -- finds the exact text "error"
"function add" -- finds the exact text "function add"
Dot: Match Any Single Character
A . matches any one character:
"h.t" -- matches "hat", "hit", "hot", "hut"
"file." -- matches "file1", "fileA", "file_"
Star: Match Zero or More of the Previous Character
A * after a character means "zero or more of this character":
"go*d" -- matches "gd", "god", "good", "goood"
Common Combo: .* (Match Anything)
The most useful regex combo is .* which means "any characters" (like the * wildcard in Glob, but for text):
"start.*end" -- matches any line containing "start" followed later by "end"
"function.*click" -- matches "function handleClick", "function onClick", etc.
Word Boundary: \b
Use \b to match whole words only:
"\berror\b" -- matches "error" but not "errors" or "errorMessage"
For most searches, just type the exact text you want to find. Regex is there for when you need flexibility, but plain text searches cover 90% of use cases. When you ask Claude naturally ("find files containing 'TODO'"), it handles the regex for you.
Searching Specific File Types
You can narrow your search to only certain kinds of files:
> Search for "import" only in JavaScript files
> Find "color" in CSS files only
> Search for "SELECT" in .sql files
Claude will combine Grep with a file type filter, so it only searches relevant files. This is faster and gives you cleaner results when you have a large project with many different file types.
Understanding Grep Results
Here is a more detailed example of Grep output:
─ Grep: "config"
.env:1: DATABASE_CONFIG=localhost
src/app.js:3: const config = require('./config');
src/app.js:15: if (config.debug) {
src/config.js:1: module.exports = {
docs/setup.md:8: Edit the config file to change settings.
5 matches across 4 files
Claude's results tell you:
.envline 1 has a matchsrc/app.jshas two matches (lines 3 and 15)src/config.jshas a match on line 1docs/setup.mdhas a match on line 8
After the results, Claude usually summarizes: "The word 'config' appears in 4 files. Most references are in the src folder."
Grep vs Glob: A Quick Review
| Question | Tool | Example |
|---|---|---|
"Where are the .css files?" | Glob | Finds files named *.css |
| "Which files use the color blue?" | Grep | Finds files containing the text "blue" |
| "Find all test files" | Glob | Finds files matching **/*test* |
| "Which test files test the login feature?" | Grep | Finds files containing "login" in test files |
Sometimes Claude uses both together. For example, "Find all JavaScript files that import React" might first use Glob to find **/*.js files, then Grep to search those files for "import React".
Common Questions
Q: Does Grep search binary files (images, compiled programs)? No. Grep searches text-based files only. It skips images, executables, and other binary files automatically.
Q: Can Grep search for multi-line patterns? By default, Grep searches one line at a time. Claude can enable multiline mode for patterns that span lines, but for beginners, single-line searches cover nearly all needs.
Q: Is Grep case-sensitive? By default, yes. "Error" and "error" are different searches. But you can ask Claude to do a case-insensitive search: "Search for 'error' in any case" and Claude will handle it.
Q: What if there are too many results? If your search returns hundreds of matches, you can narrow it down:
- Search in a specific folder: "Search for 'error' in the src folder only"
- Search in specific file types: "Search for 'error' only in .log files"
- Use a more specific term: "Search for 'ConnectionError' instead of 'error'"
Try It Yourself
Step 1: Use Your Practice Files
If you completed the Glob lesson, you should have some practice files. If not, ask Claude to create them:
> Create these files for me:
> - src/app.js with the content: "// TODO: add login feature\nconst app = require('express');\n// Main application entry point"
> - src/utils.js with the content: "// Utility functions\nfunction formatDate(date) { return date; }\n// TODO: add more utilities"
> - docs/guide.md with the content: "# Guide\nThis guide covers the application setup.\nSee the app.js file for the entry point."
Step 2: Search for Exact Text
> Find all TODO comments in this project
You should see matches in src/app.js and src/utils.js.
Step 3: Search in Specific File Types
> Search for "app" only in JavaScript files
You should see matches in the .js files but not in guide.md.
Step 4: Try a Flexible Search
> Find any line that mentions both "function" and a name after it
Claude might use a regex like "function \w+" and find formatDate in utils.js.
Step 5: Combine Glob and Grep
> How many Markdown files do I have, and which ones mention the word "app"?
Claude may use Glob first to find .md files, then Grep to search inside them.
If you can search for text across files, narrow results by file type, and understand the output format, you have the Grep tool down. Combined with Glob, you can find anything in any project.
What You Learned
- Grep tool: Searches the text inside files across your entire project
- Results format: Each match shows the file, line number, and matching text
- Basic regex:
.matches any character,.*matches anything,\bmarks word boundaries - Filter by type: Narrow searches to specific file types like
.jsor.md - Grep vs Glob: Grep searches file contents, Glob searches file names
- Ask naturally: Describe what you are looking for and Claude handles the details
How was this lesson? Take 2 minutes to share your feedback — it helps us make the tutorials better for everyone.