Menu
Coddy logo textTech

Grep With Flags

Part of the Fundamentals section of Coddy's Terminal journey. Lesson 35 of 82.

grep becomes even more powerful when you add flags to control how it searches and what it displays.

Most useful grep flags:

-icase insensitive search. Matches regardless of uppercase or lowercase:

grep -i "error" server.log   # matches error, Error, ERROR

-n — show line numbers alongside each matching line:

grep -n "error" server.log
3:error: disk full
6:error: connection timeout

-c — show only the count of matching lines, not the lines themselves:

grep -c "error" server.log
2

-vinvert the search — show lines that do not match the pattern:

grep -v "info" server.log
error: disk full
warning: high memory usage
error: connection timeout

-r — search recursively through all files in a directory:

grep -r "error" logs/   # search all files inside the logs folder

-l — show only the filenames that contain a match, not the matching lines:

grep -l "error" *.log

Combining flags:

You can combine multiple flags together:

grep -in "error" server.log   # case insensitive + show line numbers
grep -vc "info" server.log    # invert + count non-info lines
challenge icon

Challenge

Easy

Use grep with flags to inspect server.log in three different ways:

  1. Find all lines containing error and show their line numbers using -n
  2. Count how many lines contain info using -c
  3. Show all lines that do not contain info using -v

Hint: Run all three commands one after the other. Each flag changes how grep presents its results.

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Terminal playground