How do I search code with grep?
Searching through a codebase can feel slow when you rely only on your editor’s global search. grep gives you a direct, script-friendly way to find patterns across files and folders from the terminal. It’s quick, flexible, and works well for everything from spotting function calls to hunting down configuration values.
What is grep and when should you use it?
grep searches text input for lines that match a pattern. In practice, that means you can scan source files, logs, or generated output for:
- Function or method names (
handleRequest,to_json,render) - Error strings (
"connection refused","timeout") - TODO or FIXME notes
- Imports and module usage
- Config keys (
API_KEY,timeout_ms,feature_flag)
It shines when you need repeatable searches, want results you can pipe to other commands, or need to scan large folder trees quickly.
The basic grep pattern
The simplest form is:
Bash
Example:
Bash
This prints every line in app.py that contains TODO.
If you want line numbers:
Bash
If you want the filename included (useful when searching multiple files):
Bash
Searching recursively through a project
Most code searches are recursive:
Bash
Example: find where handleLogin is used:
Bash
To include line numbers and show filenames clearly:
Bash
Common flags here:
-Rrecursive-Iskip binary files-nshow line numbers
Case-insensitive searches and whole words
To ignore case:
Bash
To match whole words only (so cat doesn’t match concatenate):
Bash
These help reduce noisy matches in large repositories.
Using regular expressions for smarter matching
grep supports regular expressions. Two common modes:
- Basic regex (default)
- Extended regex with
-E(often easier)
Example: find import statements for either requests or httpx:
Bash
Example: find function definitions in Python (simple pattern):
Bash
Example: find lines that end with a semicolon (useful in mixed code or generated files):
Bash
Showing context around matches
Sometimes the matching line isn’t enough. Add context:
-C Nshows N lines before and after-B Nshows N lines before-A Nshows N lines after
Example: show 3 lines around matches for deprecated:
Bash
This is handy for reading surrounding logic without opening a file.
Filtering by file type and excluding folders
Limit the search to certain file patterns using --include:
Bash
Exclude generated or vendor directories:
Bash
Exclude files by name pattern:
Bash
This keeps results focused and avoids scanning large dependency trees.
Inverting matches and counting results
To show lines that do not match a pattern:
Bash
To count matches per file:
Bash
To list only filenames that contain the match:
Bash
To list filenames that do not contain the match:
Bash
Practical workflows for code search
A few reliable day-to-day searches:
-
Find hardcoded secrets (then verify before acting):
Bash -
Locate old endpoints or routes:
Bash -
Find usage of a class or symbol:
Bash -
Review all TODOs in only source folders:
Bash
Closing thoughts
grep is a strong tool for code search because it’s predictable: you can refine patterns, limit scope, add context, and combine it with other command-line tools. Once you get comfortable with recursive searches, file filtering, and regex, it becomes a natural part of reading and maintaining code.












