Console9

grep flags and regex reference

Complete reference for grep command-line flags, regular expression syntax, and character classes.

Complete reference for grep flags covering search modes, output control, context display, and regular expression syntax for BRE and ERE.

grep Search Mode Flags

Flaggrep behaviorDefaultExample
-Egrep uses Extended Regular Expressions (ERE) — `,+ ,? , () , {}` work without escapingBRE
-Fgrep matches fixed strings — no regex interpretation, faster for literal patternsBRE`grep -F "error
-Pgrep uses Perl-Compatible Regular Expressions (PCRE) — adds \d, \w, lookahead, lookbehindBREgrep -P "\d{4}-\d{2}-\d{2}" log.txt
-igrep matches case-insensitivelyCase-sensitivegrep -i "error" log.txt
-wgrep matches whole words only — pattern must be bounded by non-word charactersSubstring matchgrep -w "error" log.txt (won't match "errors")
-xgrep matches entire lines only — the pattern must match the complete lineSubstring matchgrep -x "exact line" file.txt

grep Output Control Flags

Flaggrep behaviorDefaultExample
-cgrep prints the count of matching lines instead of the lines themselvesPrint linesgrep -c "error" log.txt
-lgrep prints only filenames containing matchesPrint linesgrep -rl "TODO" /project/
-Lgrep prints only filenames NOT containing matchesPrint linesgrep -rL "TODO" /project/
-ngrep prefixes each matching line with its line numberNo numbersgrep -n "error" log.txt
-vgrep inverts the match — prints lines that do NOT match the patternNormal matchgrep -v "debug" log.txt
-ogrep prints only the matched portion of each line, not the entire lineFull linegrep -oE "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" access.log

grep Context Flags

Flaggrep behaviorDefaultExample
-A {n}grep prints n lines AFTER each match for context0grep -A 3 "error" log.txt
-B {n}grep prints n lines BEFORE each match for context0grep -B 2 "error" log.txt
-C {n}grep prints n lines BEFORE and AFTER each match (combined context)0grep -C 5 "error" log.txt

grep File Selection Flags

Flaggrep behaviorDefaultExample
-rgrep searches all files in the directory tree recursivelySingle filegrep -r "pattern" /etc/
--include=GLOBgrep searches only files matching the glob pattern during recursive searchAll filesgrep -r --include="*.conf" "listen" /etc/
--exclude=GLOBgrep skips files matching the glob patternNonegrep -r --exclude="*.log" "pattern" /var/
-Igrep skips binary files during searchProcess allgrep -rI "pattern" /var/log/