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
- Use grep -F for literal searches.
- Use --include="*.js" to restrict file types.
- ripgrep (`rg`) is often faster for large repositories.
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
Find lines matching a pattern in files or piped input — the workhorse of text search.
findLocate files anywhere in a directory tree by name, type, size, date — and act on them.
xargsTurn a list of items into command arguments — the glue between pipelines and commands.
Concepts involved: Regular expression · Pipe (|)
More how-to guides
See how much space is left, and find out what is using it.
Extract a .tar.gz archiveUnpack tar.gz, tar.bz2, tar.xz and zip archives from the command line.
Free a port that is already in useFind which process holds a port and stop it cleanly.
Find and replace text in filesReplace text in one file or across a whole project, safely.