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.

Cheat sheet

Use flags to control how grep searches and displays results.

-i — case insensitive search:

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

-n — show line numbers:

grep -n "error" server.log

-c — show count of matching lines:

grep -c "error" server.log

-v — invert search (show non-matching lines):

grep -v "info" server.log

-r — search recursively through directories:

grep -r "error" logs/

-l — show only filenames containing matches:

grep -l "error" *.log

Flags can be combined:

grep -in "error" server.log   # case insensitive + line numbers
grep -vc "info" server.log    # invert + count

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