Whether you’re just starting with Linux or you’re a seasoned developer, the grep
command is an essential tool in your command-line arsenal. If you need linux commands for common task, you can read this article, Linux CheatSheet for common tasks
In this article, we’ll walk through:
- What
grep
is - Beginner-friendly
grep
examples - Intermediate and advanced
grep
use cases - Practical real-world tips
Let’s dive in!
✅ What is GREP in Linux?
grep
stands for Global Regular Expression Print. It’s used to search for patterns or specific text in one or more files. It supports regular expressions and comes with powerful options for filtering, highlighting, and recursive searching.
🟢 Section 1: grep
for Beginners – Getting Started
📌 Basic Syntax
grep [OPTIONS] PATTERN [FILE...]
🧪 Basic Examples
Example 1: Search for a word
grep "apple" fruits.txt
Output:
apple
pineapple
Example 2: Case-insensitive search
grep -i "apple" fruits.txt
Example 3: Show line numbers
grep -n "apple" fruits.txt
Output:
1:apple
4:pineapple
Example 4: Exclude matches
grep -v "apple" fruits.txt
Example 5: Count matches
grep -c "apple" fruits.txt
🟡 Section 2: Intermediate grep
Usage
🔁 Recursive search in directories
grep -r "TODO" /home/user/code/
📁 Limit file types during search
grep -r --include="*.py" "import" .
📌 Search multiple patterns (OR)
grep -E "error|fail" server.log
🔎 Match whole words
grep -w "is" file.txt
🔍 Match lines that start or end with text
grep "^start" file.txt # Starts with "start"
grep "end$" file.txt # Ends with "end"
🔴 Section 3: Advanced grep
Usage for Power Users
🧠 1. Use grep with Perl Regex (PCRE)
grep -P "\d{4}-\d{2}-\d{2}" file.txt
Matches dates in YYYY-MM-DD
format.
🧰 2. Use grep in scripts with exit codes
if grep -q "error" server.log; then
echo "Error found in log"
fi
📂 3. Exclude files or directories
grep -r "search" . --exclude-dir={node_modules,venv} --exclude=*.log
🧪 4. Combine grep
with find
find . -type f -name "*.html" -exec grep "meta" {} +
🧵 5. Pipe output to grep for filtering
ps aux | grep "nginx"
📌 6. Highlight matches
grep --color=auto "error" server.log
📋 7. Match exact lines only
grep -x "Success" result.txt
🧱 8. Fixed string search (no regex)
grep -F "foo.bar" file.txt
🧩 Cheatsheet: Common grep
Options
Option | Description |
---|---|
-i | Ignore case |
-v | Invert match |
-r / -R | Recursive search |
-n | Show line numbers |
-c | Count of matches |
-w | Match whole word |
-x | Match entire line |
-l / -L | Show files with / without matches |
--color | Highlight matched text |
-F | Fixed string (faster, no regex) |
-P | Perl-compatible regex |
--exclude | Exclude specific files |
--include | Include specific files |
📌 Real-World Use Case: Search Logs for Errors
grep -i "error" /var/log/nginx/access.log | grep -v "404"
✅ Find all error messages excluding 404s.
📦 grep Alternatives (Bonus Tip)
Tool | Use Case |
---|---|
egrep | Same as grep -E |
fgrep | Same as grep -F |
zgrep | Search inside .gz files |
pgrep | Search process names (not text in files) |
🏁 Conclusion
The grep
command is a powerful ally for searching logs, config files, and code. From simple text search to complex regular expressions, grep
handles it all.
🎯 Whether you’re a Linux beginner or an advanced user, knowing how to wield
grep
saves time, improves productivity, and makes you look like a pro.
No Comments
Leave a comment Cancel