AskHandle

AskHandle Blog

How do I search code with grep?

January 24, 2026Melissa Olson3 min read
  • Grep
  • Search
  • Code

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
1grep "pattern" file

Example:

bash
1grep "TODO" app.py

This prints every line in app.py that contains TODO.

If you want line numbers:

bash
1grep -n "TODO" app.py

If you want the filename included (useful when searching multiple files):

bash
1grep -H "TODO" app.py

Searching recursively through a project

Most code searches are recursive:

bash
1grep -R "pattern" .

Example: find where handleLogin is used:

bash
1grep -R "handleLogin" .

To include line numbers and show filenames clearly:

bash
1grep -RIn "handleLogin" .

Common flags here:

  • -R recursive
  • -I skip binary files
  • -n show line numbers

Case-insensitive searches and whole words

To ignore case:

bash
1grep -Ri "error" .

To match whole words only (so cat doesn’t match concatenate):

bash
1grep -Rw "cat" .

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
1grep -REn "import (requests|httpx)" .

Example: find function definitions in Python (simple pattern):

bash
1grep -REn "^def [a-zA-Z_][a-zA-Z0-9_]*" .

Example: find lines that end with a semicolon (useful in mixed code or generated files):

bash
1grep -REn ";$" .

Showing context around matches

Sometimes the matching line isn’t enough. Add context:

  • -C N shows N lines before and after
  • -B N shows N lines before
  • -A N shows N lines after

Example: show 3 lines around matches for deprecated:

bash
1grep -RIn -C 3 "deprecated" .

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
1grep -RIn "timeout" . --include="*.js"

Exclude generated or vendor directories:

bash
1grep -RIn "timeout" . --exclude-dir=node_modules --exclude-dir=dist --exclude-dir=build

Exclude files by name pattern:

bash
1grep -RIn "timeout" . --exclude="*.min.js"

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
1grep -RIn -v "DEBUG" .

To count matches per file:

bash
1grep -RIn -c "TODO" .

To list only filenames that contain the match:

bash
1grep -RIl "TODO" .

To list filenames that do not contain the match:

bash
1grep -RIL "TODO" .

A few reliable day-to-day searches:

  • Find hardcoded secrets (then verify before acting):

    bash
    1grep -RIn "API_KEY" . --exclude-dir=node_modules
  • Locate old endpoints or routes:

    bash
    1grep -RIn "/v1/" .
  • Find usage of a class or symbol:

    bash
    1grep -RInw "UserService" .
  • Review all TODOs in only source folders:

    bash
    1grep -RIn "TODO" src tests

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.