What does grep do?
grep searches text and prints only the lines that match a word or regular expression. It is the most used command for log analysis.
grep [options] pattern [file…]
Most useful options
| Option | What it does |
|---|---|
| -i | ignore upper/lower case |
| -n | show line numbers |
| -v | show lines that do NOT match |
| -c | count matching lines |
| -r | search all files in folders |
| -o | print only the matching part |
| -E | extended regex (+, ?, |, {n}) |
| -l | only list file names |
grep examples
In security work
SOC analysts grep logs for attack patterns; pentesters grep -r for passwords and API keys left in code.
Questions about grep
How do I search for text in all files in Linux?
grep -r "text" /path — add -i to ignore case and -l to list only file names.
How do I count matches with grep?
grep -c pattern file counts matching lines.