grep advanced (advanced)

Advanced grep: recursive search, context, PCRE patterns, and file filtering.

low risk

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

Beyond basic pattern matching, grep supports recursive directory search, context lines around matches, Perl-compatible regex (PCRE), and filtering by file type. Used heavily for log analysis, code search, and configuration auditing.

Common flags
FlagMeaning
-r/-RRecursive directory search
-l --files-with-matchesPrint only filenames, not matches
-c --countPrint match count per file
-A/-B/-CLines After/Before/Context of match
-P --perl-regexpPerl-compatible regex (PCRE)
--includeOnly search files matching pattern
-v --invert-matchPrint non-matching lines
Examples
grep -rn 'password' /etc/ --include='*.conf' 2>/dev/nullFind password references in all config files · explain & risk-check →
grep -A 5 -B 2 'CRITICAL' /var/log/app.logShow 5 lines after and 2 before each CRITICAL match · explain & risk-check →
grep -P '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' access.logExtract IPv4 addresses using PCRE · explain & risk-check →
grep -rL 'strict_mode' /etc/mysql/conf.d/Find config files that DON'T have strict_mode · explain & risk-check →
grep -c 'ERROR' /var/log/*.log | sort -t: -k2 -rn | head -5Files with most ERROR lines · explain & risk-check →
Pro tip

Use `grep -rn 'pattern' . --include='*.py' --exclude-dir='.git'` for code search — the include/exclude flags prevent searching binaries and version control directories.

Related
awk advanced sed advanced find
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.