grep

Search text using patterns — print lines matching a regex from files or stdin.

low risk

interactive breakdown — edit the command and press Explain
$ 
What it does

grep filters lines matching a regular expression. -r scans directories recursively, -v inverts to show non-matching lines, -P enables Perl-compatible regex. Use --include to scope recursive searches to specific file types.

Common flags
FlagMeaning
-r --recursiveSearch recursively through directories
-i --ignore-caseCase-insensitive matching
-v --invert-matchPrint lines that do NOT match
-n --line-numberPrefix output with line numbers
-l --files-with-matchesOnly print filenames with at least one match
-c --countCount matching lines per file
-E --extended-regexpUse extended regex (ERE)
-P --perl-regexpUse Perl-compatible regex for advanced patterns
--includeScope recursive search to files matching a glob
Examples
grep -rn 'ERROR' /var/log/ --include='*.log'Find ERROR lines with line numbers across all log files · explain & risk-check →
grep -v '^#' /etc/nginx/nginx.confShow nginx config without comment lines · explain & risk-check →
grep -P 'Failed password.*from (\d+\.){3}\d+' /var/log/auth.logExtract failed SSH login IPs using Perl regex · explain & risk-check →
Pro tip

Use `grep -c ''` to count total lines in a file — faster than `wc -l` when already in a grep pipeline.

Related
awk sed find less
Open the full explainer →More in Text Processing

Reference text is hand-written and may be incomplete — check man grep for your version. Test destructive commands on a non-production system first.