Search text using patterns — print lines matching a regex from files or stdin.
low risk
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.
| Flag | Meaning |
|---|---|
-r --recursive | Search recursively through directories |
-i --ignore-case | Case-insensitive matching |
-v --invert-match | Print lines that do NOT match |
-n --line-number | Prefix output with line numbers |
-l --files-with-matches | Only print filenames with at least one match |
-c --count | Count matching lines per file |
-E --extended-regexp | Use extended regex (ERE) |
-P --perl-regexp | Use Perl-compatible regex for advanced patterns |
--include | Scope recursive search to files matching a glob |
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 →Use `grep -c ''` to count total lines in a file — faster than `wc -l` when already in a grep pipeline.
Reference text is hand-written and may be incomplete — check man grep for your version. Test destructive commands on a non-production system first.