Linux / Bash
Beginner
1 min read
Searching Text with grep
Example
# Basic pattern search
grep "error" /var/log/syslog
# Case-insensitive search
grep -i "error" /var/log/syslog
# Show line numbers
grep -n "failed" /var/log/auth.log
# Invert match — lines NOT containing pattern
grep -v "^#" /etc/ssh/sshd_config # Show non-comment lines
# Count matching lines
grep -c "GET" /var/log/nginx/access.log
# List only filenames that contain a match
grep -rl "password" /etc/
# Recursive search with context (2 lines before and after)
grep -rn -C 2 "TODO" /var/www/html/
# Extended regex — match multiple patterns
grep -E "error|warning|critical" /var/log/syslog
# Match whole words only
grep -w "root" /etc/passwd
# Fixed string search (no regex, faster)
grep -F "192.168.1.1" /var/log/nginx/access.log
# Show only the matched portion, not the whole line
grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" /var/log/nginx/access.log | sort | uniq -c | sort -rn
# Exclude binary files
grep -I "pattern" /usr/bin/*
# Search with colour highlighting
grep --color=auto "FAILED" /var/log/auth.log
Related Resources
Linux / Bash Reference
Complete tag & property list
Linux / Bash How-To Guides
Step-by-step practical guides
Linux / Bash Exercises
Practice what you've learned
More in Linux / Bash