Sandbox99 Chronicles

Grep Command Cheat Sheet – Supercharge Your Productivity

grep cheatsheet

Written by Jose Mendez

Hi, I’m Jose Mendez, the creator of sandbox99.cc. with a passion for technology and a hands-on approach to learning, I’ve spent more than fifteen years navigating the ever-evolving world of IT.

Published Aug 11, 2025 | Last updated on Aug 11, 2025 at 6:39PM

Reading Time: 2 minutes

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.

PatternExample CommandWhat It Matches
Match multiple words`grep -E “errorfail
Match lines starting with a wordgrep -E "^INFO" app.logLines that start with INFO
Match lines ending with a wordgrep -E "end$" data.txtLines that end with end
Match a range of numbersgrep -E "ID: [0-9]{3}" records.txtID: followed by exactly 3 digits
Match an email addressgrep -E "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-z]{2,}" contacts.txtSimple email pattern
Match IPv4 addressgrep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" logs.txtAny IPv4-like string
Match empty linesgrep -E "^$" file.txtBlank lines
Match non-empty linesgrep -E ".+" file.txtLines with at least one character

💡 Quick Reference Table

OptionDescription
-iIgnore case
-rRecursive search
-nShow line numbers
-wMatch whole words
-cCount matches
-vInvert match
--includeSearch only matching file patterns
--excludeSkip matching file patterns
--exclude-dirSkip entire directories
-EUse extended regex
-C numShow 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.

Calendar

September 2025
S M T W T F S
 123456
78910111213
14151617181920
21222324252627
282930  

Related Post

How to Add AppImage Applications to the XFCE4 Menu

How to Add AppImage Applications to the XFCE4 Menu

✍️ Brief Introduction Managing applications on Linux can sometimes feel fragmented, especially when dealing with portable packages like AppImage that don’t integrate into the desktop menu by default. Unlike .deb or .rpm packages, AppImages run as standalone...

read more