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

OptionDescription
-iIgnore case
-vInvert match
-r / -RRecursive search
-nShow line numbers
-cCount of matches
-wMatch whole word
-xMatch entire line
-l / -LShow files with / without matches
--colorHighlight matched text
-FFixed string (faster, no regex)
-PPerl-compatible regex
--excludeExclude specific files
--includeInclude 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)

ToolUse Case
egrepSame as grep -E
fgrepSame as grep -F
zgrepSearch inside .gz files
pgrepSearch 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.

https://ituptodate.com
Do you like Ravikant Kushwaha's articles? Follow on social!
Comments to: Mastering the grep Command in Linux – From Beginner to Advanced

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.


Login

Welcome to ITUptodate

Brief and amiable onboarding is the first thing a new user sees in the theme.
Read Smart, Save Time
Pick all the topics you are interested in to fill your homepage with stories you'll love.

Join ITUptodate