Skip to contentExploitQuest

Finding the Needle with grep

The single most-reached-for tool on the command line does one thing perfectly: show me only the lines that matter. Learn grep and haystacks stop being a problem.

4 min readNot yet reviewed
Wrenlearner

A log file has ten thousand lines. I need the three about a failed login. Do I scroll?

Rookmentor

You never scroll. You ask grepgrepThe search tool: it reads lines and prints only the ones that match a pattern you give it. to hand you only the lines you want. It is the tool you will use more than any other, because "show me the lines that matter" is the most common wish there is.

Show me only the matching lines

grep pattern file reads the file and prints only the lines containing the pattern. Place it after a pipePipeThe "|" that connects two commands, sending the output of the first straight in as the input of the second. and it filters whatever the previous command produced instead. Either way, the job is the same: keep the lines that match, drop the rest.

From ten thousand lines to the three that matter
# only the lines mentioning a failed login
$ grep "Failed password" auth.log
Aug 24 09:14 sshd: Failed password for wren from 203.0.113.9
Aug 24 09:14 sshd: Failed password for root from 203.0.113.9
Aug 24 09:15 sshd: Failed password for root from 203.0.113.9
# count them instead of reading them
$ grep -c "Failed password" auth.log
3
grep -i   pattern file    -> case-insensitive (Error, error, ERROR all match)
grep -v   pattern file    -> invert: show lines that do NOT match
grep -n   pattern file    -> show the line number of each match
grep -r   pattern folder/ -> search every file under a folder
  1. Line 2-v is quietly one of the most useful: "show me everything except the noise" is how you strip out the lines you already know you can ignore.
  2. Line 4-r turns grep from "search this file" into "search this whole project", which is how you find where a word appears across hundreds of files.

You want every line in a log that mentions one IP address, but not the routine "connection closed" ones. How do you get there with grep and a pipe?

What does `grep -vi "debug" app.log` show?

The everyday shape

Reading everything

- Opening a huge log and scrolling to find something
- Skimming past thousands of irrelevant lines by eye
- Missing the one line that mattered in the noise

Filtering to the point

- grep for the term, and read only what matches
- grep -v to remove the noise you already recognise
- Stack greps through pipes to narrow to the exact lines
- grep -c to count instead of read, when you only need a number

The mindset shift is the whole lesson: you do not read data to find things, you ask a tool to bring you only the things. Haystacks stop mattering when you never look at the hay.

Wrenlearner

So I stop reading logs and start querying them.

Rookmentor

That is exactly the shift. A defender who greps their auth log spots the break-in attempt in seconds. The skill is neutral and universal.

Magpieadversary

It is the first thing I run on anything I am handed. Pipe it, grep it, and the signal falls out of the noise. We use the same tool from opposite chairs.

Now find it yourself

Your turn

Somewhere in this folder is a line that starts with FLAG. There are hundreds of lines and only one matches. Print just that line.

you@practice
Practice shell — nothing here is real. Type 'help' to begin.

So grep answers the most common wish on the command line — show me only what matters — and it composes through pipes into filters of any precision. With the pipe and grep you can already interrogate real data. That closes Pipes and Filters; the free Linux course has given you a genuine working footing in the shell every later course and lab will stand on.