grep prints the lines of a file that match what you are looking for. Learn -n for line numbers, -i to ignore case and -r to search a whole directory, by running the commands in a real terminal in your browser.
Updated: 2026-09-05
grep [options] pattern file...
Reads a file line by line and prints only the lines that match. Lines that do not match are simply not shown.
The terminal on this page has a log file and some source code.
$ grep ERROR app.log
ERROR connection refused
ERROR timeout
Out of five lines, the two containing ERROR came back.
That is the whole idea.
-n tells you where in the file each match is.
$ grep -n ERROR app.log
3:ERROR connection refused
5:ERROR timeout
Those numbers are the ones your editor uses, so you can jump straight there.
-i matches regardless of case.
$ grep -i todo README.md
Todo: write the setup guide.
Useful whenever a word is written inconsistently.
-r walks everything below a directory, and each match tells you which file it came from.
$ grep -r TODO .
./src/index.js:// TODO: handle errors
Options combine.
$ grep -ri todo src
src/index.js:// TODO: handle errors
src/utils.js:// todo: add tests
-v inverts the result and prints the lines that did not match.
$ grep -v INFO app.log
WARN slow query
ERROR connection refused
ERROR timeout
Strip the ordinary lines out of a log and what remains is the interesting part.
grep is how you pull the handful of relevant lines out of a mountain of them.
| Situation | What you type |
|---|---|
| Find the failures in a log | grep ERROR app.log |
| Find where a function is defined | grep -rn "createUser" src |
| Just count the matches | grep -c ERROR app.log |
| Narrow another command's output | ls -l | grep ".json" |
-c gives you the count on its own.
$ grep -c ERROR app.log
2
No output means no match, not a failure.
$ grep nope app.log
Before assuming a typo, check the capitalisation (-i) and whether you are searching the right place.
Quote patterns that contain spaces.
grep connection refused app.log treats refused as a filename.
grep "connection refused" app.log keeps it as one pattern.
It searches contents, not names.
When you want to find files by name, that is find.
webterm.appthis site
Terminal Fundamentals
Learn commonly used commands
learn.webterm.appa separate site

Commands stick when they show up in a real sequence of work, not one at a time. There is a course that builds them up in order.
See the course