DevGuide

How to search text in files with grep

On this page
  1. Search one file
  2. Search a whole project
  3. Just the filenames
  4. The flags worth knowing

To search text in files with grep, the core command is grep 'pattern' file, and grep -r 'pattern' . searches every file under the current folder. That covers most of what you reach for: finding where a function is defined, which config sets a value, or what is filling up a log. A few flags do the heavy lifting: -i ignores case, -n adds line numbers, -l prints only the filenames that matched, and -v flips it to show the lines that do not match. It is on every Linux and macOS box already. Here's the everyday command, the flags worth committing to memory, and how to grep a whole project without drowning in noise.

The short answer

grep 'pattern' file searches one file, grep -r 'pattern' . searches everything below the current folder. Add -i to ignore case, -n for line numbers, -l for filenames only, -v to invert the match.

grep -rsearch a whole tree
-i / -nignore case / line numbers
-ljust the filenames
Answer card showing grep -r to search text across files, with -i, -n and -l flags.
One command finds where anything lives in code or logs. The flags shape the output. PNG

Search one file

Linux
grep "error" app.log

Every line containing error prints. Quote the pattern when it has spaces or shell characters in it.

Search a whole project

Linux
grep -rin "todo" src/

-r recurses into subfolders, -i ignores case (so TODO and todo both hit), -n prints the line number next to each match. This is the one I type most.

Just the filenames

When you only care which files match, not the lines:

Linux
grep -rl "API_KEY" .
Terminal showing grep -rin finding TODO comments with line numbers, then grep -rln listing only the filenames.
Lines first, then just the files. -l is the difference. PNG

The flags worth knowing

A short list carries you a long way: -w for whole words, -v to show lines that do not match, -c to count hits instead of printing them, -C 3 for three lines of context around each match, and -F to search a literal string when the pattern has regex characters you want taken at face value.

Frequently asked questions

How do I search every file in a folder and its subfolders?

Add -r (recursive): "grep -r pattern .". The dot means start from here. Add -n for line numbers and -i to ignore case, so "grep -rin pattern ." is the combination most people settle on.

How do I show only the names of files that match?

Use -l: "grep -rl pattern .". Instead of every matching line, you get one filename per file that contains the pattern, which is what you want when you are about to open or edit them.

How do I search for a whole word, not a substring?

Use -w. Searching for cat normally also matches category and concatenate; "grep -w cat" matches only the standalone word. Pair it with -i when case does not matter.

grep treats my search term like a pattern and matches odd things. Why?

By default grep reads the term as a basic regular expression, so characters like dot, star and brackets have special meaning. To search for a literal string, add -F (fixed): it finds the exact text and treats those characters as plain.