HappiomHappiom
  • Self-Improvement
  • Relationship
  • AI for Life
  • Apps
  • Tech
  • More
    • Online Diary
    • Glossary
  • Learn
    • Book
    • >> Soft Skills
    • Time Management
    • >> Tech Skills
    • R
    • Linux
    • Python
  • Our Apps
    • Download Diary App
    • Write Your First Diary
    • Login to Online Diary App
    • 100K+ Famous Quotes Site
  • Resources
    • Self-Improvement Guide
      • 21-Days to Self-Improvement
      • Creating a Habit
      • Learn Life Experiences
      • Easily Prioritizing Tasks
      • Learning from Mistakes
      • Doing Regular Exercises
      • Setting Priority for Success
      • Avoiding Common Mistakes
      • Eating Healthy Food Regularly
    • Journaling Guide
      • Online Diary
      • Best Diary Apps
      • Diary Writing Ideas
      • Diary Writing Topics
      • Avoid Writing in Diary
      • Diary Writing as Hobby
      • Reasons to Write a Diary
      • Types of Feelings In Diary
      • Improve Diary Writing Skills
  • Self-Improvement
  • Relationship
  • AI for Life
  • Apps
  • Tech
  • More
    • Online Diary
    • Glossary
  • Learn
    • Book
    • >> Soft Skills
    • Time Management
    • >> Tech Skills
    • R
    • Linux
    • Python
  • Our Apps
    • Download Diary App
    • Write Your First Diary
    • Login to Online Diary App
    • 100K+ Famous Quotes Site
  • Resources
    • Self-Improvement Guide
      • 21-Days to Self-Improvement
      • Creating a Habit
      • Learn Life Experiences
      • Easily Prioritizing Tasks
      • Learning from Mistakes
      • Doing Regular Exercises
      • Setting Priority for Success
      • Avoiding Common Mistakes
      • Eating Healthy Food Regularly
    • Journaling Guide
      • Online Diary
      • Best Diary Apps
      • Diary Writing Ideas
      • Diary Writing Topics
      • Avoid Writing in Diary
      • Diary Writing as Hobby
      • Reasons to Write a Diary
      • Types of Feelings In Diary
      • Improve Diary Writing Skills
Expand All Collapse All
  • Linux Examples
    • 10 Crontab Commands and Examples to Automate Tasks in Linux
    • 10 Linux Commands to Manage Users and Permissions Effectively
    • 10 Practical find Command Examples Every Linux User Should Know
    • 10 Linux Commands to Monitor System Performance - CPU, RAM, Disk, etc.
    • 10 Linux Archive and Compression Commands
    • 10 Disk Usage Commands to Find and Clean Up Space in Linux
    • 10 grep Command Examples to Supercharge Your Searches
    • 10 Networking Commands in Linux for Troubleshooting and Monitoring
    • 10 File Management Commands in Linux for Daily Use
    • 10 Linux Commands to Kill, Pause, and Manage Processes
    • 10 Linux Commands to Search Files Recursively Like a Pro
    • 10 Essential Linux Commands for Searching Files
    • 5 Quick Linux Commands to Find Disk Space
    • Difference between $? vs $@ in Linux Shell Scripting

10 grep Command Examples to Supercharge Your Searches

grep is one of the most powerful and flexible commands in Linux. Whether you’re searching through code, analyzing logs, or filtering outputs in pipelines, grep can save you hours of manual work. It’s fast, supports regular expressions, and works beautifully with other Unix tools. In this guide, we’ll look at 10 practical and advanced grep examples that will take your searching skills to the next level. Each command is explained with clear use-cases and real-world outputs, helping you master text searching like a pro.

1. Search for Exact Matches in Files

This is the most basic use of grep—search for a literal string inside a file.

$ grep "main()" /home/myuser/code/main.c
int main() {
  

It shows lines containing the string “main()”. grep highlights results when your terminal supports it.

2. Search Recursively in a Directory

Use -r or -R to search through all files under a directory recursively.

$ grep -R "api_key" /home/myuser/projects
/home/myuser/projects/config/settings.py:api_key = "XYZ123"
  

This is very useful when you’re looking for variable names or constants across a codebase.

3. Show Line Numbers with Matches

To include the line number where the match appears, use the -n flag.

$ grep -n "server" config.yaml
42:  server: "192.168.1.100"
  

This is helpful when you’re editing files based on search results.

4. Search Case-Insensitive

The -i flag makes the search case-insensitive.

$ grep -i "error" /var/log/syslog
Jul 6 14:32:01 server CRON[29219]: (root) CMD (some_script.sh) — Error occurred
  

Great for searching logs or user input where case may vary.

5. Count Matching Lines

Use -c to count how many lines matched the pattern.

$ grep -c "ERROR" /var/log/app.log
58
  

This is ideal for summarizing log data or checking how many times an event occurred.

6. Show Only the Matching Part of the Line

-o extracts only the part of the line that matches the pattern—not the full line.

$ grep -o "http[s]*://[^ ]*" urls.txt
https://example.com
http://testsite.net
  

This is excellent when you’re extracting URLs, IP addresses, or any pattern using regex.

7. Use Regular Expressions

grep supports full regular expressions by default, or use grep -E for extended regex (equivalent to egrep).

$ grep -E "error|fail|fatal" /var/log/app.log
2025-07-06 12:01:12 - fatal: service failed to start
  

This allows you to match multiple patterns in a single command.

8. Invert the Match (Exclude Lines)

Use -v to exclude lines that match a pattern. It inverts the result.

$ grep -v "DEBUG" app.log > app_no_debug.log
  

This is helpful when you want to filter out noise, like debug lines in logs.

9. Display Lines Before or After a Match

Use -A, -B, or -C to show context around matches.

$ grep -C 2 "panic" server.log
123: Starting service...
124: Connecting to database
125: panic: unexpected connection failure
126: Restarting service...
127: Alert triggered
  

-C 2 shows 2 lines before and after. Use -A or -B for just after or before, respectively.

10. Combine with Other Commands Using Pipes

grep becomes even more powerful when combined with other commands like ps, netstat, or ls using pipes.

$ ps aux | grep firefox
myuser   3192  5.0  3.2 998344 132000 ?   Sl   10:10   0:40 /usr/lib/firefox/firefox
  

This technique lets you filter real-time output to locate exactly what you need.

Bonus: Highlight Pattern Matches

Most modern versions of grep highlight matches in color when outputting to a terminal. If it’s not working, try adding --color=auto.

$ grep --color=auto "auth" auth.log
  

This visually helps to spot matches quickly in a large chunk of output.

Conclusion

grep is much more than a basic search command—it’s a versatile powerhouse that can handle everything from simple string searches to complex pattern matching across massive files. Whether you’re debugging logs, analyzing code, or filtering structured output from pipelines, knowing how to wield grep effectively can dramatically boost your productivity in the Linux terminal.

Related Articles
  • 10 Crontab Commands and Examples to Automate Tasks in Linux
  • 10 Linux Commands to Manage Users and Permissions Effectively
  • 10 Practical find Command Examples Every Linux User Should Know
  • 10 Linux Commands to Monitor System Performance – CPU, RAM, Disk, etc.
  • 10 Linux Archive and Compression Commands
  • 10 Disk Usage Commands to Find and Clean Up Space in Linux

No luck finding what you need? Contact Us

Previously
10 Disk Usage Commands to Find and Clean Up Space in Linux
Up Next
10 Networking Commands in Linux for Troubleshooting and Monitoring
  • About Us
  • Contact Us
  • Archive
  • Hindi
  • Tamil
  • Telugu
  • Marathi
  • Gujarati
  • Malayalam
  • Kannada
  • Privacy Policy
  • Copyright 2025 Happiom. All Rights Reserved.