grep — Search & Filter Lines

What is grep?

grep (Global Regular Expression Print) prints lines that match a pattern. It’s the first tool in almost every filter chain.

OSCP use: Hunt passwords in configs, filter nmap output, extract relevant lines from logs before passing to awk or cut.


Syntax

grep [OPTIONS] PATTERN [FILE...]
command | grep PATTERN

📌 Common Flags

FlagDescription
-iCase insensitive
-vInvert — show lines that do NOT match
-r / -RRecursive — search directories
-nShow line numbers
-lList filenames only (files containing match)
-LList filenames that do not contain match
-cCount matching lines per file
-wMatch whole word only
-xMatch whole line only
-EExtended regex (egrep) — +, ?, |
-PPerl regex (PCRE) — lookaheads, etc.
-A NShow N lines after match
-B NShow N lines before match
-C NShow N lines before and after
-oPrint only the matching part
--color=autoHighlight matches (often default)
-m NStop after N matches
-e PATTERNSpecify pattern (useful for patterns starting with -)
-f FILERead patterns from file

📌 Basic Usage

# Search a file
grep "password" config.php
grep -i "admin" access.log
 
# Search recursively
grep -r "password" /var/www/
grep -rni "secret" /etc/ 2>/dev/null
 
# Invert — exclude lines
grep -v "nologin" /etc/passwd
grep -v "false\|nologin" /etc/passwd
 
# List files containing match
grep -rl "password" /var/www/ 2>/dev/null
 
# Count matches
grep -c "Failed password" /var/log/auth.log
 
# From pipe
cat access.log | grep "404"
nmap -p- TARGET | grep "^[0-9]"

📌 Regex Examples

# OR (extended regex)
grep -E "password|passwd|secret" config.xml
 
# Lines starting with digit (nmap ports)
grep "^[0-9]" ports.txt
 
# IP address pattern (basic)
grep -E "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" log.txt
 
# Extract matching part only
grep -oE "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" log.txt
 
# Context around match
grep -C 3 "database" config.php

📌 OSCP Examples

# Find config files with passwords
grep -rni "password\|passwd\|db_pass" /var/www/ 2>/dev/null
 
# Interactive users only
grep -v "nologin\|false" /etc/passwd
 
# Failed SSH logins
grep "Failed password" /var/log/auth.log
 
# Filter nmap open ports
grep "/tcp.*open" nmap.txt
 
# Chain to awk
grep "jab.htb" xmpp.txt | awk -F@ '{print $1}'

📌 Quick Cheat Sheet

grep "pattern" file.txt
grep -ri "password" /path/ 2>/dev/null
grep -v "exclude" file.txt
grep -rl "pattern" /dir/
grep -E "pat1|pat2" file.txt
grep -o "extract-this" file.txt
cat file | grep "pattern"

📌 Windows equivalents (findstr / Select-String)

On Windows, the closest equivalents to grep are findstr (CMD) and PowerShell Select-String.

Select-String -Path .\all-utf8.txt `
  -Pattern "password|passwd|secret|token|apikey|api_key|ssh|PRIVATE KEY|INSERT INTO|CREATE DATABASE|CREATE TABLE|admin|user|login" `
  -AllMatches -CaseSensitive:$false
 
# With line numbers
Select-String -Path .\all-utf8.txt `
  -Pattern "password|passwd|secret|token|apikey|api_key|ssh|PRIVATE KEY|INSERT INTO|CREATE DATABASE|CREATE TABLE|admin|user|login" `
  -AllMatches -CaseSensitive:$false | Format-Table LineNumber, Line -AutoSize

CMD (findstr)

findstr /I /N /R "password passwd secret token apikey api_key ssh admin user login PRIVATE CREATE INSERT" all-utf8.txt
LinuxWindows
grep -ifindstr /I or Select-String -CaseSensitive:$false
grep -nfindstr /N or Format-Table LineNumber
grep -E "a|b"findstr /R or Select-String -Pattern "a|b"
grep -rGet-ChildItem -Recurse | Select-Stringgci

Windows CMD - Powershell Commands · gci