Mission 8 of 30

Grep: Search Master

Find the important lines in a noisy application log.

Search text confidently with exact matches, case-insensitive matches, inverted matches, and simple regex.

40–50 min Week 2 · Search, text, permissions, processes Ubuntu terminal
Your outcome: You can find information instead of scrolling endlessly.
Practice inside ~/linux-practice. Check your setup before you begin. Prerequisite: Pipes and Filters .

What you’ll practice

  • Use grep options
  • Understand simple regex anchors
  • Search recursively in practice files

The mental model

  • grep prints lines matching a pattern.
  • Regex is a pattern language; start small.
  • -r searches folders recursively.

Try it in your terminal

Basic grep

Safe in practice folder

Finds matching and non-matching lines.

Run from ~/linux-practice

cd ~/linux-practice
echo -e 'Apple\napple\nAPPLE\nBanana\napricot' > fruits.txt
grep -i 'apple' fruits.txt
grep -v 'apple' fruits.txt

Regex anchors

Safe in practice folder

Uses start, end, and OR matching.

Run from ~/linux-practice

grep '^A' fruits.txt
grep 'e$' fruits.txt
grep -E 'apple|Banana' fruits.txt

Recursive search

Safe in practice folder

Searches through folders.

Run from ~/linux-practice

mkdir -p projects/search-demo
echo 'secret pattern' > projects/search-demo/file.txt
grep -r 'pattern' projects

Put it into practice

  1. Create ips.txt and find addresses starting with 192.168.
  2. Use grep -n to show line numbers.

Check your result

Only the intended log lines appear.

Common mistakes

  • You type `grep apple|banana file.txt` and get confused by the results. The shell sees the pipe symbol and tries to run two commands. Wrap your pattern in quotes: `grep 'apple|banana' file.txt`. Quotes tell grep, 'this whole thing is my search pattern.'

Check your understanding

Explain these in your own words. If you’re unsure, revisit the examples before moving on.

  1. Which option ignores case?
  2. What does ^ mean in a regex?

Your practice, your pace

Loading saved progress…

Discussion

Continue the conversation about this guide. Loading comments connects to GitHub Discussions through giscus.

Open community discussions on GitHub

Check before you copy

This command needs care. Read its context, confirm the target and location, and keep a recovery path.

Copying does not run a command.