Lesson 3.2

Searching Content with grep

8 minutes

Searching Inside Files

In the last lesson, you learned to find files by their name. But what if you need to find files based on what’s inside them? That’s what grep does — it searches the contents of files for a specific piece of text.

AI Tool Connection
grep is probably the single most-used command by AI coding tools. When your AI assistant needs to understand how a function is used across your codebase, or find where a variable is defined, it reaches for grep.

Basic Usage

The simplest form of grep searches a single file for a word or phrase:

grep "search term" filename

Try it now: Type grep "TODO" ~/projects/my-app/src/index.js

If the file contains the word “TODO” on any line, grep will print those lines. If there’s no match, you’ll see nothing — silence means “not found.”

The Flags That Matter

Plain grep is useful, but the real power comes from its flags. Here are the ones you’ll use constantly:

-r (Recursive)

Search through all files in a directory and its subdirectories:

grep -r "TODO" ~/projects/my-app/src/

This searches every file inside src/ and all folders within it. Without -r, grep only looks at the specific file you name.

Try it now: Type grep -r "TODO" ~/projects/my-app/src/

-n (Line Numbers)

Show which line number each match is on:

grep -n "error" ~/projects/my-app/logs/error.log

The output will look something like:

3:  [ERROR] Failed to connect to database
17: [ERROR] Timeout waiting for response

Those numbers on the left tell you exactly where to look in the file.

-i (Case-Insensitive)

Match regardless of uppercase or lowercase:

grep -i "error" ~/projects/my-app/logs/error.log

This matches “error”, “Error”, “ERROR”, and any other combination. Very useful when you’re not sure how something was capitalized.

Try it now: Type grep -i "error" ~/projects/my-app/logs/error.log

-c (Count)

Instead of showing the matching lines, just tell you how many there are:

grep -c "ERROR" ~/projects/my-app/logs/error.log

This outputs a single number — the count of matching lines.

Try it now: Type grep -c "ERROR" ~/projects/my-app/logs/error.log

-l (Files Only)

When searching recursively, sometimes you just want to know which files contain a match, not every matching line:

grep -rl "TODO" ~/projects/my-app/src/

This prints only the filenames, one per line. It’s a quick way to get an overview.

Combining Flags

Flags can be combined. The most common combination you’ll see is -rn:

grep -rn "TODO" ~/projects/my-app/src/

This searches recursively (-r) and shows line numbers (-n) — so you get the file path and the line number for every match. This is the go-to command for searching a codebase.

Try it now: Type grep -rn "TODO" ~/projects/my-app/src/

Practical Examples

Here are some real-world scenarios:

Find all TODO comments in a project:

grep -rn "TODO" ~/projects/my-app/src/

Check how many errors are in a log:

grep -c "ERROR" ~/projects/my-app/logs/error.log

Find which files import a specific module:

grep -rl "import" ~/projects/my-app/src/

Search case-insensitively for a function name:

grep -rni "handleclick" ~/projects/my-app/src/

Practice

Try these in the terminal:

  1. grep -rn "TODO" ~/projects/my-app/src/ — find all TODOs with locations
  2. grep -i "error" ~/projects/my-app/logs/error.log — search for errors (any case)
  3. grep -c "ERROR" ~/projects/my-app/logs/error.log — count the errors
  4. grep -rl "TODO" ~/projects/my-app/src/ — list just the files with TODOs
BlueBox Terminal