UnixDroid

Search · Linux command

grep command in Linux

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

OptionWhat it does
-iignore upper/lower case
-nshow line numbers
-vshow lines that do NOT match
-ccount matching lines
-rsearch all files in folders
-oprint only the matching part
-Eextended regex (+, ?, |, {n})
-lonly list file names

grep examples

$ grep "Failed password" /var/log/auth.log Try it ›

failed SSH logins

$ grep -c error app.log Try it ›

count errors

$ grep -ri password /var/www Try it ›

hunt for hard-coded passwords

$ grep -oE "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" access.log Try it ›

extract IP addresses

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.