Chapter 19: Bash Search Text (grep)

What does grep actually do? (super simple first)

grep = global regular expression print It searches for text patterns inside files (or from input) and prints the matching lines.

Think of it as Ctrl+F on steroids for the terminal.

You give it:

  • A word / phrase / pattern to find
  • One or more files (or pipe input)

It shows every line that contains your pattern.

Most used tool by developers, sysadmins, data people – because logs, code, configs are all text!

1. Basic usage (start here – type these now!)

Create a small test file first (copy-paste):

Bash

Now search:

Bash

Output:

text

It printed whole lines that contain “Hyderabad”.

2. Very common options (these 5 you will use 90% of time)

Option What it does Example command Why useful?
-i Ignore case (case-insensitive) grep -i “hyderabad” notes.txt Finds “Hyderabad”, “hyderabad”, “HYDERABAD”
-n Show line numbers grep -n “bash” notes.txt Know exactly which line
-r or -R Recursive – search inside folders too grep -r “error” /var/log/ Search entire project / logs
-l Show only filenames (not lines) grep -rl “python” ~/projects/ Quick list of files that have the word
-v Invert – show lines that do NOT match grep -v “Hyderabad” notes.txt Exclude boring lines

Try these:

Bash

3. Search patterns – make it smarter (regular expressions basics)

By default grep uses basic regular expressions (BRE). Add -E for extended (ERE) – easier!

Common patterns:

Pattern example Command example What it finds
^Hyderabad grep “^Hyderabad” notes.txt Lines starting with “Hyderabad”
2026$ grep “2026$” notes.txt Lines ending with “2026”
error|warning grep -E "error warning" log.txt
error[0-9] grep “error[0-9]” log.txt error followed by digit (error1, error5…)
bash.*power grep “bash.*power” notes.txt “bash” then anything then “power”
[0-9]{6} grep -E “[0-9]{6}” notes.txt Exactly 6 digits (like pin code 500081)

Try:

Bash

4. Color & context (make output beautiful & useful)

Bash

Super useful for logs:

Bash

5. Count & quiet mode

Bash

-q = quiet → use in scripts (if grep finds something → $? = 0)

6. Real-world examples you will use daily

  • Find your IP in config:
Bash
  • Check running processes:
Bash
  • Search code for function:
Bash
  • Find emails in text:
Bash
  • Find TODO comments in code:
Bash

7. Quick cheat-sheet table (print this or save!)

Goal Command example
Simple search grep “word” file.txt
Case insensitive grep -i “word” file.txt
With line numbers grep -n “word” file.txt
Recursive (folders) grep -r “word” /path/
Only filenames grep -rl “word” .
Show context (before/after) grep -C 3 “error” log.txt
OR pattern `grep -E “error
Lines starting with… grep “^Error” log.txt
Count matches grep -c “warning” *.log
Exclude matches grep -v “debug” app.log
Highlight + recursive + line num grep -rin –color=auto “bug” src/

8. Pro tips from daily use

  • Always quote patterns if they have spaces: grep “hello world” file
  • Use grep -E (or egrep) for modern regex (pipes |, +, ? work without escape)
  • Pipe output: cat log.txt | grep error | wc -l (count errors)
  • Combine with other tools: ls -l | grep “Aug” (files modified in August)
  • Very fast even on huge files (GBs of logs)

Now open your terminal and try these 3 right now:

Bash

Tell me what you see! Or ask:

  • “How to find all Python files that import requests?”
  • “How to grep only error lines from journalctl?”
  • “Explain regex for phone numbers in India”

We’ll build the exact command together! 😄

You may also like...

Leave a Reply

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