grep
grep prints lines that match a pattern. It is the standard Unix pattern-matching tool, available in three regular-expression dialects: basic (BRE), extended (ERE), and Perl-compatible (PCRE, when compiled in).
Essential options
# Case-insensitive match
grep -i "pattern" file.txt
# Recursive search through a directory tree
grep -r "pattern" /path/to/dir
# Invert match (show lines that do NOT match)
grep -v "pattern" file.txt
# Show line numbers
grep -n "pattern" file.txt
# Count matches only
grep -c "pattern" file.txt
# Extended regex (unescaped +, ?, |, etc.)
grep -E "pattern+" file.txtRegular expression notes
- In BRE (default), the metacharacters
?,+,{,|,(, and)lose their special meaning; escape them with\?,\+,\{,\|,\(,\)to enable them. - In ERE (
-E), those metacharacters are special by default. - PCRE (
-P) enables lookaheads, lookbehinds, and non-greedy quantifiers where supported. \wmatches word characters ([0-9a-zA-Z_]); see regex-metacharacters for the full class table.
Related: findstr (Windows equivalent), regex-metacharacters, sed-single-line-find-replace.
Sources
- [grep(1) — Linux manual pagegrep(1) — Linux manual page