Skip to content
zero2vibecodelearn vibe coding
Level 3 · Search and pipes 6 min Reviewed by the author · Aug 4, 2026

Searching inside files: grep

`grep` prints the lines of a file that contain the text you are looking for. It turns a thousand-line log into the four lines that matter.

Level 3 · 0 / 4 lessons0%

find locates files by name. grep looks inside them.

grep pattern file

grep ERROR server.log
ERROR database timeout
ERROR disk full

The file had six lines. Two of them contained ERROR, so those two came back — in full, exactly as they appear in the file.

That is grep’s entire model: go through the file line by line, keep the lines that contain the pattern, print them.

Note

The name comes from an old text-editor command, g/re/p. Nobody remembers that, and the word has long since become a verb — people say “grep the logs” the way they say “google it”.

Quick check

A file has 500 lines and 3 of them contain the word timeout. What does `grep timeout file.log` print?

The four flags worth knowing

FlagEffect
-iignore case, so ERROR and error both match
-nshow the line number of each match
-cprint only how many lines matched
-vinvert it: show the lines that do not match

-v is the underrated one. When a log is 95 percent routine noise, filtering the noise out is often faster than describing the signal:

grep -v INFO server.log

Quick check

You want every line of a log EXCEPT the ones containing DEBUG. Which flag?

Silence means zero matches

grep TODO notes.txt

If nothing comes back, nothing matched. That is a real answer, not a failure — and it is different from an error, which would print something like No such file or directory.

Tip

When a search comes back empty and you expected results, try -i first. Roughly half the time the text is there and the capitalisation is not what you assumed.

Quick check

grep returns nothing at all. How do you tell 'no matches' from 'something went wrong'?

Try it

The log has six lines: four routine, two problems.

  1. cat server.log — read the whole thing, so you can see what grep is filtering.
  2. grep ERROR server.log — the two lines that matter.
  3. grep -i error server.log — the same result, but now it would also catch Error or error.

Then experiment: grep -v INFO server.log, grep -n ERROR server.log, grep -c INFO server.log. Six lines is small enough to check every answer by eye, which is the best way to build trust in a filter.

Check your understanding

Answered 0/3
Mode:

What does `grep` print?

What does the `-i` flag do?

You run `grep TODO notes.txt` and get nothing back. What does that mean?

Finish the task in the terminal and answer the questions to complete the lesson.

📝 My notes

Saved automatically in this browser · shows up on the Notes page.

Task

Pull the ERROR lines out of server.log, then do it again ignoring case.

Tab complete · history · Ctrl+L clear · help commands

  • Not done: Search for ERROR in server.log
  • Not done: Only the matching lines come back
  • Not done: Search again ignoring case

Lesson Q&A

What does `grep` print?

grep is line-oriented. A line either contains the pattern or it does not, and matching lines are printed in full.

What does the `-i` flag do?

i is for ignore-case. Useful whenever you are not sure how the text was capitalised.

You run `grep TODO notes.txt` and get nothing back. What does that mean?

No matches means no output. A missing file would have printed an error instead.

A file has 500 lines and 3 of them contain the word timeout. What does `grep timeout file.log` print?

grep keeps whole lines. Counting is a separate flag, -c.

You want every line of a log EXCEPT the ones containing DEBUG. Which flag?

-v flips the filter: keep what does not match.

grep returns nothing at all. How do you tell 'no matches' from 'something went wrong'?

Unix tools are silent on success and loud on failure. No message at all means grep ran fine and found nothing.

Move between lessons with · search with ⌘K