Cheatsheet: grep
Last updated 2026-06-21
Basic searching
Search for a pattern in a file
Search case-insensitively
Show matching line numbers
Show only the matching text
Count matching lines
Recursive and file filtering
Search recursively under a directory
Show only files that contain a match
Show only files that do not contain a match
Search only matching file names
Exclude a directory during recursive search
Matching modes
Invert the match to show non-matching lines
Match a whole word
Match a whole line exactly
Treat the pattern as a fixed string, not a regex
Use extended regular expressions
Use multiple patterns
Context and pipelines
Show lines after each match
Show lines before each match
Show lines before and after each match
Search command output through a pipe
See also: