Introduction

When you’re working with Linux or macOS, the grep command is one of the most powerful tools in your arsenal for finding text fast. Whether you’re hunting down a config option in /etc, locating error messages in logs, or digging through source code, grep can save you hours.

This cheat sheet will help you master grep for maximum productivity — from basic usage to advanced regex magic.

🔍 Basic Syntax

grep [OPTIONS] "pattern" file_or_path

- pattern – the text or regex you’re looking for
- file_or_path – the file(s) or directory to search in
- OPTIONS – flags that modify the behavior of grep


# 1. Basic Search in a Single File
# Finds lines containing "hello" in file.txt.
grep "hello" file.txt

# 2. Case-Insensitive Search
# Matches "Hello", "HELLO", "hello".
grep -i "hello" file.txt

# 3. Search in All Files in a Directory (Recursive)
# Searches "error" in all files, including subdirectories.
grep -r "error" /var/log/

# 4. Show Line Numbers
# Displays line numbers where the pattern is found.
grep -n "main" app.py

# 5. Search Only in Specific File Types
# Looks inside only .log files, recursively.
grep -rn --include="*.log" "timeout" /var/log/

# 6. Exclude Certain Files or Folders
# Skips backup files or entire directories.
grep -rn --exclude="*.bak" "db_password" /configs/
grep -rn --exclude-dir="cache" "refresh_token" /project/

# 7. Match Whole Words Only
# Matches cat but not concatenate.
grep -rw "cat" animals.txt

# 8. Count Matches Instead of Showing Lines
# Returns the number of matching lines.
grep -c "404" access.log

# 9.  Invert the Match (Show Non-Matching Lines)
# Shows lines without "DEBUG".
grep -v "DEBUG" app.log

# 10. Use Extended Regular Expressions
# Matches lines containing either "error" or "failed"
grep -E "error|failed" server.log

# 11. Search for a Pattern and Show Context Lines
# Shows 3 lines before and after each match.
grep -C 3 "panic" system.log

# 12. Pipe grep with Other Commands
# Filters running processes for "nginx".
ps aux | grep "nginx"

🛠 Pro Tip – Combine with less for Scrolling

grep -rn "config" /etc/ | less

Lets you navigate large search results easily.


📌 Grep + Regex Quick Examples (Advanced Productivity)

grep supports regular expressions (regex) to match complex patterns. Combine with -E for extended regex.

Pattern Example Command What It Matches
Match multiple words `grep -E "error fail
Match lines starting with a word grep -E "^INFO" app.log Lines that start with INFO
Match lines ending with a word grep -E "end$" data.txt Lines that end with end
Match a range of numbers grep -E "ID: [0-9]{3}" records.txt ID: followed by exactly 3 digits
Match an email address grep -E "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-z]{2,}" contacts.txt Simple email pattern
Match IPv4 address grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" logs.txt Any IPv4-like string
Match empty lines grep -E "^$" file.txt Blank lines
Match non-empty lines grep -E ".+" file.txt Lines with at least one character

💡 Quick Reference Table

Option Description
-i Ignore case
-r Recursive search
-n Show line numbers
-w Match whole words
-c Count matches
-v Invert match
--include Search only matching file patterns
--exclude Skip matching file patterns
--exclude-dir Skip entire directories
-E Use extended regex
-C num Show context lines

🚀 Conclusion

grep is not just a text search tool — it’s a productivity powerhouse for developers, sysadmins, and security researchers. With the right flags and regex, you can filter massive data sets in seconds.

Keep this cheat sheet handy, and you’ll never waste time manually scanning files again.