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):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
cat > notes.txt << 'EOF' Hello Arman from Hyderabad! This is line 2 with bash and grep. Python is fun too. Hyderabad - Telangana - 500081 Error: something went wrong 2026 bash scripting is powerful Hyderabad weather today: sunny EOF |
Now search:
|
0 1 2 3 4 5 6 |
grep "Hyderabad" notes.txt |
Output:
|
0 1 2 3 4 5 6 7 8 |
Hello Arman from Hyderabad! Hyderabad - Telangana - 500081 Hyderabad weather today: sunny |
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:
|
0 1 2 3 4 5 6 7 8 |
grep -in "error" notes.txt # case insensitive + line numbers grep -r "bash" . # search current folder + subfolders grep -rl "2026" . # which files have "2026"? |
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:
|
0 1 2 3 4 5 6 7 8 |
grep -E "^[A-Z]" notes.txt # lines starting with capital letter grep -E "[0-9]{6}" notes.txt # find pin codes grep -E "error|warning|critical" /var/log/syslog |
4. Color & context (make output beautiful & useful)
|
0 1 2 3 4 5 6 7 8 9 |
grep --color=auto "bash" notes.txt # highlights match in red (auto = only when terminal) grep -A 2 "error" log.txt # show match + 2 lines After grep -B 3 "error" log.txt # show 3 lines Before grep -C 2 "error" log.txt # Context: 2 before + 2 after |
Super useful for logs:
|
0 1 2 3 4 5 6 |
grep -C 5 "failed login" /var/log/auth.log |
5. Count & quiet mode
|
0 1 2 3 4 5 6 7 8 |
grep -c "Hyderabad" notes.txt # count how many lines match grep -c -i "error" *.log # count errors in all log files grep -q "success" build.log && echo "Build OK" # quiet – no output, just exit status |
-q = quiet → use in scripts (if grep finds something → $? = 0)
6. Real-world examples you will use daily
- Find your IP in config:
|
0 1 2 3 4 5 6 |
grep -i "ip addr" ~/.ssh/config |
- Check running processes:
|
0 1 2 3 4 5 6 7 |
ps aux | grep -i chrome # find chrome processes ps aux | grep -v grep | grep python # exclude the grep itself! |
- Search code for function:
|
0 1 2 3 4 5 6 |
grep -r -n "def calculate_total" ~/projects/ |
- Find emails in text:
|
0 1 2 3 4 5 6 |
grep -E "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}" contacts.txt |
- Find TODO comments in code:
|
0 1 2 3 4 5 6 |
grep -r "# TODO" . |
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:
|
0 1 2 3 4 5 6 7 8 |
grep -i "hyderabad" notes.txt grep -n -E "[0-9]{6}" notes.txt grep -r "TODO" ~/Documents/ # if you have code/notes |
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! 😄
