Linux command list

grep command: find text inside files

__ __ _ _____
\ \ / /__| |_|_ _|__ _ __ _ __ ___
\ \ /\ / / _ \ '_ \| |/ _ \ '__| '_ ` _ \
\ V V / __/ |_) | | __/ | | | | | |
\_/\_/ \___|_.__/|_|\___|_| |_| |_| |_
 
A sandbox for trying grep. Nothing here can touch your real files.
user@webterm:~/project$
 

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

Syntax

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.

Try it first

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.

Showing line numbers

-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.

Ignoring capitalisation

-i matches regardless of case.

$ grep -i todo README.md
Todo: write the setup guide.

Useful whenever a word is written inconsistently.

Searching a whole directory

-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

Printing what does not match

-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.

When you actually reach for it

grep is how you pull the handful of relevant lines out of a mountain of them.

SituationWhat you type
Find the failures in a loggrep ERROR app.log
Find where a function is definedgrep -rn "createUser" src
Just count the matchesgrep -c ERROR app.log
Narrow another command's outputls -l | grep ".json"

-c gives you the count on its own.

$ grep -c ERROR app.log
2

Things that trip people up

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.

Practise it hands-on

webterm.appthis site

learn.webterm.appa separate site

>_WEBTERM LEARN

WebTerm Learn: from one command to actually using it

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

Related commands

find / cat / less / tail

Frequently asked questions

What does grep stand for?
It comes from the ed editor command g/re/p, meaning globally search for a regular expression and print. The name is the operation.
Nothing was printed. Did it fail?
No, that means nothing matched. grep stays quiet rather than reporting an error when there are no matches.
Why do I get an error when I point it at a directory?
By default grep only reads files. Add -r to search everything inside a directory.
When should I use grep and when find?
grep looks inside files, find looks for the files themselves by name or type. What is written in it, or where is it.
Can I ignore capitalisation?
Yes, -i does that. It matters for logs where ERROR and error both appear.