CmdKeys LogoCommand Quiz

Grep Command Practice

grep prints every line that matches a pattern; flags change which files, which case, and how much context you see. This track drills -r, -i, -v, -n, and combined searches you type in a repo, not a GUI find box.

Command Reference

basic

CommandDescription
grep <pattern> <file>Print all lines in a file that contain the pattern.
grep -c <pattern> <file>Print the number of matching lines instead of the lines themselves.
grep -i <pattern> <file>Search for a pattern ignoring upper/lower case differences.
grep -v <pattern> <file>Print all lines that do NOT match the pattern.
grep -w <pattern> <file>Match only complete words, not partial word occurrences.

output

CommandDescription
grep -n <pattern> <file>Display each matching line prefixed with its line number in the file.
grep -l <pattern> <file>Print only the names of files that contain at least one match, not the matching lines.
grep -o <pattern> <file>Print only the exact portion of each line that matches the pattern.
grep -q <pattern> <file>Run silently with no output — exits with status 0 if a match is found.
grep -m <num> <pattern> <file>Stop reading a file after finding the specified number of matching lines.

recursive

CommandDescription
grep -r <pattern> <dir>Search for a pattern in all files under a directory, recursively.
grep -ri <pattern> <dir>Recursively search a directory for a pattern, ignoring case.
grep -rn <pattern> <dir>Recursively search a directory and display the line number for each match.
grep -rl <pattern> <dir>Recursively search a directory and print only the names of files that contain a match.

regex

CommandDescription
grep -E '<pattern>' <file>Search using extended regular expressions, enabling operators like +, ?, and |.
grep -E '<pattern1>|<pattern2>' <file>Print lines matching either of two patterns using alternation.
grep '^<pattern>' <file>Print only lines where the pattern appears at the very beginning of the line.
grep '<pattern>$' <file>Print only lines where the pattern appears at the very end of the line.
grep '^$' <file>Print all empty lines in a file.
grep -e '<pattern>' <file>Specify a pattern explicitly, useful when the pattern begins with a dash.
grep '[[:alpha:]]' <file>Match lines containing at least one letter (upper or lower case) using a POSIX class.
grep '[[:digit:]]' <file>Match lines containing at least one numeric digit using a POSIX class.
grep '[[:space:]]' <file>Match lines containing at least one whitespace character using a POSIX class.

context

CommandDescription
grep -A <num> <pattern> <file>Print each matching line plus N lines of trailing context after it.
grep -B <num> <pattern> <file>Print each matching line plus N lines of leading context before it.
grep -C <num> <pattern> <file>Print each matching line with N lines of context both before and after it.
grep -A 3 '<pattern>' <file>Show 3 lines of trailing context after each match — commonly used to read log exceptions.

combined

CommandDescription
grep -ro <pattern> <dir>Recursively search all files and print only the matched portion of each line.
grep -rni <pattern> <dir>Recursively search a directory, show line numbers, and ignore case — all at once.
grep -vc <pattern> <file>Count the number of lines that do NOT match the pattern.
grep -Ew '<pattern>' <file>Match complete words only, using extended regex syntax.

Key Use Cases

  • Search a whole tree for a symbol without opening files
  • Print lines that do NOT match a pattern
  • Show N lines of context around each hit
  • Restrict matches to a file glob or skip binary files
  • Count matches per file instead of printing every line

Frequently Asked Questions

What does grep -v actually return?

It prints lines that fail the match. Exit status is still 0 if any non-matching line exists, which surprises people who treat grep like a boolean.

When should I use -E or -P instead of basic regex?

Basic grep needs escaped grouping. -E enables extended regex (egrep). -P is PCRE where available. This track tests the flag you type, not the engine name.

How is grep -r different from find piped to xargs grep?

-r walks the tree itself and can skip binaries. A find pipeline needs extra flags for spaces and is slower for simple recursive search.

Why practice grep if ripgrep exists?

grep is on every POSIX box and in scripts. rg is faster locally; this track is the portable flag set you still need over ssh.

Ready to master Grep commands?

Test your muscle memory with our spaced-repetition quiz system. Free forever.

Start Practice Now →