Suggested day 820–30 minutesSafe in practice folder

Mission 3 of 5

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.

Outcome: You can find information instead of scrolling endlessly.

Mental model

Understand what is happening

grep prints lines matching a pattern.

Regex is a pattern language; start small.

-r searches folders recursively.

Guided lab

Work through the mission

1

Basic grep

Safe

Finds matching and non-matching lines.

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

Regex anchors

Safe

Uses start, end, and OR matching.

Terminal
grep '^A' fruits.txt
grep 'e
#x27; fruits.txt grep -E 'apple|Banana' fruits.txt
3

Recursive search

Safe

Searches through folders.

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

Checkpoint

Verify the mission

Only the intended log lines appear.

Try it yourself

One step without a recipe

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

Need a hint?

Start every grep session with the simplest pattern that works, then add complexity. `grep 'error'` before `grep -E 'error|warning'`. Get the basics right, then layer on regex. Trying to write the perfect pattern on the first attempt is how you end up staring at a blank prompt for ten minutes.

Explain it back

Before you move on

  • Which option ignores case?
  • What does ^ mean in a regex?

Shortcuts:Alt + ← previous lessonAlt + → next lessonAlt + H home? focus search