Linux GREP Interactive Tutorial

Master the power of pattern searching in Linux

🐧

GREP Command Reference

Basic Usage

grep [OPTION] PATTERN [FILE...] # Search for pattern in files grep "error" logfile.txt # Find lines containing "error" grep -i "Error" logfile.txt # Case insensitive search

Common Options

-i # Ignore case -v # Invert match (show non-matching) -n # Show line numbers -c # Count matches -l # List filenames with matches -r # Recursive search -E # Extended regex (ERE)

Wildcards & Quantifiers

. # Any single character * # Zero or more of preceding + # One or more of preceding ? # Zero or one of preceding {n} # Exactly n times {n,} # n or more times {n,m} # Between n and m times

Position Anchors

^ # Beginning of line $ # End of line \< # Start of word \> # End of word \b # Word boundary

Character Classes

[abc] # Any of a, b, or c [a-z] # Any lowercase letter [A-Z] # Any uppercase letter [0-9] # Any digit [^abc] # NOT a, b, or c [:alpha:] # Alphabetic characters [:digit:] # Numeric characters

🔐 Personal Information Patterns

[0-9]{3}-[0-9]{2}-[0-9]{4} # SSN (XXX-XX-XXXX) \([0-9]{3}\) [0-9]{3}-[0-9]{4} # Phone (XXX) XXX-XXXX [0-9]{3}-[0-9]{3}-[0-9]{4} # Phone XXX-XXX-XXXX [0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4} # Credit Card XXXX-XXXX-XXXX-XXXX [0-9]{5}(-[0-9]{4})? # ZIP Code XXXXX or XXXXX-XXXX [0-9]{1,2}/[0-9]{1,2}/[0-9]{4} # Date MM/DD/YYYY [A-Z]{2}[0-9]{6,8} # Driver's License (varies by state)

🛡️ Security & Forensics Patterns

([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}) # MAC Address [A-Fa-f0-9]{32} # MD5 Hash [A-Fa-f0-9]{40} # SHA1 Hash [A-Fa-f0-9]{64} # SHA256 Hash \\\\[a-zA-Z0-9.-]+\\[a-zA-Z0-9.-]+ # UNC Path \\server\share C:\\[a-zA-Z0-9\\._-]+ # Windows File Path /[a-zA-Z0-9/._-]+ # Linux File Path

Security & Networking Use Cases

grep "Failed password" /var/log/auth.log # Find failed login attempts grep -E "ERROR|FATAL" app.log # Find errors in application logs netstat -an | grep ":80 " # Check port 80 connections ps aux | grep apache # Find Apache processes grep -r "ssh" /etc/ # Search SSH config files

Interactive GREP Simulator

Results will appear here...

Real-World Examples for IT Students

🔍 Log Analysis

Search system logs for errors, failed logins, and security events

grep -i "error\|fail" /var/log/messages

🌐 Network Monitoring

Monitor network connections and identify suspicious activity

netstat -an | grep ":22\|:80\|:443"

🔒 Security Auditing

Find failed authentication attempts and security breaches

grep "Failed password" /var/log/auth.log

⚙️ Configuration Files

Search configuration files for specific settings

grep -r "^Port\|^Listen" /etc/

💻 Process Management

Find specific processes and their resource usage

ps aux | grep -v grep | grep apache

🔢 IP Address Validation

Extract and validate IP addresses from logs

grep -E "\b[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b"

🔐 PII/PHI Detection

Identify sensitive personal and protected health information

grep -E "[0-9]{3}-[0-9]{2}-[0-9]{4}"

🦠 Malware Detection

Detect malicious activity and suspicious processes

grep -E "ALERT|powershell.*-enc|evil"

🌍 Web Server Analysis

Analyze HTTP logs for attacks and anomalies

grep -E '" [45][0-9][0-9] |sqlmap'

🗄️ Database Monitoring

Monitor database performance and security issues

grep -E "ERROR|injection|Slow query"

🐳 Container Logs

Monitor Docker containers and microservices

grep -E "level=(error|fatal)"

🪟 Windows Events

Analyze Windows Event Log for security events

grep -E "Event ID: (4625|1116)"

📊 JSON/API Logs

Parse structured JSON logs from modern applications

grep -E 'level":"(error|fatal)'

⚡ Performance Monitor

Track system performance metrics and alerts

grep -E "CPU: [89][0-9]|ALERT|CRITICAL"

Common Patterns for IT Professionals

Use Case Pattern Description
Email Addresses [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} Match valid email formats
IP Addresses \b([0-9]{1,3}\.){3}[0-9]{1,3}\b Match IPv4 addresses
MAC Addresses ([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}) Match MAC address formats
URLs https?://[^\s]+ Match HTTP/HTTPS URLs
Port Numbers :(6[0-5][0-5][0-3][0-5]|[1-5][0-9]{4}|[1-9][0-9]{0,3}) Match valid port ranges
Social Security Numbers [0-9]{3}-[0-9]{2}-[0-9]{4} Match SSN format XXX-XX-XXXX
Phone Numbers \([0-9]{3}\) [0-9]{3}-[0-9]{4} Match phone format (XXX) XXX-XXXX
Credit Cards [0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4} Match credit card format
MD5 Hashes [A-Fa-f0-9]{32} Match 32-character MD5 hashes
SHA256 Hashes [A-Fa-f0-9]{64} Match 64-character SHA256 hashes

Pattern Visualizer

Pattern matches will be highlighted here...

Pattern Breakdown:

Enter a pattern to see its explanation

Test Your GREP Knowledge

Question 1: What does the pattern "log$" match?

a) Lines ending with "log"
b) Lines containing "log"
c) Lines beginning with "log"
d) The word "log" followed by a dollar sign

Question 2: Which option makes grep case-insensitive?

a) -v
b) -i
c) -n
d) -c

Question 3: What does the character class [^0-9] match?

a) Any digit from 0 to 9
b) Numbers from 0 to 9
c) The characters 0 through 9
d) Any character that is NOT a digit

Question 4: Which command finds failed SSH login attempts?

a) grep "ssh" /var/log/auth.log
b) grep "login" /var/log/auth.log
c) grep "Failed password" /var/log/auth.log
d) grep -v "Accepted" /var/log/auth.log

Question 5: What does [0-9]{3} match?

a) Exactly three digits
b) Any three characters
c) Up to three digits
d) Three or more digits

Question 6: What does the pattern "^Error" match?

a) Any line containing "Error"
b) Lines ending with "Error"
c) The word "Error" only
d) Lines beginning with "Error"

Question 7: Which option recursively searches directories?

a) -l
b) -r
c) -n
d) -E

Question 8: Which command counts the number of lines containing "ERROR" in a file?

a) grep -n "ERROR" file.txt
b) grep -l "ERROR" file.txt
c) grep -c "ERROR" file.txt
d) grep -v "ERROR" file.txt

Question 9: What does the -v option do in grep?

a) Inverts the match (shows non-matching lines)
b) Shows version information
c) Enables verbose mode
d) Validates the pattern syntax

Question 10: Which command finds all IP addresses in a log file?

a) grep "192.168" logfile
b) grep "[0-9]" logfile
c) grep -i "ip" logfile
d) grep -E "\\b[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\b" logfile