How to Use grep Recursively in Linux

Search every file under a directory with grep, show filenames and line numbers, and skip noisy folders.

Recursive grep is the classic Linux answer to “where is this text used?”. Start with the smallest useful directory and add filters to reduce noise.

Step by step

1 Search recursively

$ grep -rn "TODO" .

Prints the filename, line number and matching line.

2 Search a project directory

$ grep -rn "DATABASE_URL" ./src

A narrower start path makes results faster and easier to inspect.

3 Ignore case

$ grep -rni "error" logs/

Combine recursion, line numbers and case-insensitive matching.

4 Exclude noisy folders

$ grep -rn "TODO" . --exclude-dir=.git --exclude-dir=node_modules

Avoid dependency and repository metadata content.

5 List matching files

$ grep -rl "deprecated-api" src/

Use -l when you only need filenames.

Tips worth knowing

Frequently asked questions

What does grep -r do?

It recursively searches files below the directory you provide.

How do I ignore node_modules?

Add `--exclude-dir=node_modules`.

How do I get line numbers?

Add `-n`, for example `grep -rn "text" .`.

The commands behind it

grep

Find lines matching a pattern in files or piped input — the workhorse of text search.

find

Locate files anywhere in a directory tree by name, type, size, date — and act on them.

xargs

Turn a list of items into command arguments — the glue between pipelines and commands.

Concepts involved: Regular expression · Pipe (|)

More how-to guides

Check disk space in Linux

See how much space is left, and find out what is using it.

Extract a .tar.gz archive

Unpack tar.gz, tar.bz2, tar.xz and zip archives from the command line.

Free a port that is already in use

Find which process holds a port and stop it cleanly.

Find and replace text in files

Replace text in one file or across a whole project, safely.