grep command examples
Updated · Every command on this page ran in our practice shell
Short answer
grep pattern file prints every line of the file that contains the pattern. Add -i to ignore case, -n for line numbers, -v to show lines that don't match, -r to search a whole folder and -c to count matching lines.
grep prints the lines of a file that match a pattern. It's the fastest way to answer questions like "where did this error happen?" or "which notes mention the supplier?". Every example below runs on a small web server log and some notes; try them in the mini terminal.
Search a file for a word
The basic form is grep pattern file. grep prints each matching line in full, in the order it appears in the file.
learner@shellstreak:~$ grep 404 logs/access.log 2026-03-02 09:15:22 GET /menu.html 404 2026-03-02 09:19:12 GET /menu.html 404
Ignore upper and lower case with -i
grep is case-sensitive: TODO doesn't match todo. Add -i to match either.
learner@shellstreak:~$ grep -i todo notes/meeting.md TODO: email the supplier todo: order more flour
Show line numbers with -n
-n puts the line number in front of each match, so you can jump straight to it in your editor.
learner@shellstreak:~$ grep -n menu logs/error.log 1:ERROR 2026-03-02 09:15:22 page not found: /menu.html 3:ERROR 2026-03-02 09:19:12 page not found: /menu.html
Show lines that don't match with -v
-v inverts the search: every line except the matches. Handy for filtering out noise, like healthy 200 responses.
learner@shellstreak:~$ grep -v 200 logs/access.log 2026-03-02 09:15:22 GET /menu.html 404 2026-03-02 09:18:55 GET /admin 403 2026-03-02 09:19:12 GET /menu.html 404 2026-03-02 09:20:30 POST /order 500
Search every file in a folder with -r
grep -r pattern folder searches all files below the folder and prefixes each match with its file name; grep -r TODO . searches the folder you're in (.). Combine flags freely: -rn gives file names and line numbers, -ri ignores case, and -l lists only the names of matching files.
learner@shellstreak:~$ grep -rn TODO notes notes/ideas.md:2:TODO: price the pies notes/meeting.md:2:TODO: email the supplier
Count matches with -c or wc -l
grep -c pattern file prints how many lines match. Piping into wc -l gives the same number and works after any command. Note that both count lines, not individual occurrences. wc -w file counts words instead, and wc -c file bytes.
learner@shellstreak:~$ grep -c ERROR logs/error.log 3
Patterns: anchors, digits and whole words
grep patterns are regular expressions. grep '^ERROR' file matches ERROR only at the start of a line, and grep '404$' file only at the end. [0-9] is any one digit and [45] is a 4 or a 5. With -E, {4} repeats the thing before it, so grep -oE 'ORD-[0-9]{4}' file prints just the order codes (-o prints only the matching part). grep -w rye file matches rye as a whole word, not inside ryebread.
Match one of several words with -E
With -E (extended patterns), | means or. Quote the pattern so the shell doesn't treat | as a pipe.
learner@shellstreak:~$ grep -E 'ERROR|WARN' logs/error.log ERROR 2026-03-02 09:15:22 page not found: /menu.html WARN 2026-03-02 09:18:55 forbidden: /admin ERROR 2026-03-02 09:19:12 page not found: /menu.html ERROR 2026-03-02 09:20:30 order failed: oven timeout
Search the output of another command
Without a file name, grep reads from a pipe. That makes it a filter you can put after almost anything: here, which of the last five requests were POSTs. (When the input is just a file, give grep the file name instead of piping cat into it.)
learner@shellstreak:~$ tail -n 5 logs/access.log | grep POST 2026-03-02 09:20:30 POST /order 500
Check yourself
One real exercise from the “Searching” unit. No account needed.Which requests in logs/access.log got a 404 (page not found)? Use grep to show those lines.
What's checked: the output of your last command.
Common mistakes
Forgetting that grep is case-sensitive. If you get nothing back, try
-i.Not quoting patterns with spaces or special characters:
grep page not found filesearches forpagein files callednotandfound. Writegrep 'page not found' file.Leaving out the folder with
grep -r. GNU grep then searches the current folder, but BSD/macOS grep reads standard input and seems to hang. Always name the folder, like..Expecting
grep -cto count every occurrence. It counts matching lines; a line with two matches counts once.
Questions
What does grep stand for?+
It comes from an old editor command, `g/re/p`: globally search for a regular expression and print matching lines.
How do I search for a whole word only?+
Use `-w`. `grep -w fig data.txt` matches `fig` but not `figure`.
How do I count words or lines in a file instead?+
Use `wc`: `wc -l file` counts lines, `wc -w file` counts words and `wc -c file` counts bytes.
Reading is step one. Now make it stick.
Practise this in a five-minute lesson with instant feedback. No account needed.