Practical Linux, Windows Server and cloud guides for IT pros.

Grep and Regex Cheat Sheet for Linux Admins

A practical grep and regex cheat sheet for Linux and RHCSA-style administration tasks, grouped by matching, extraction, counting, context, recursive search, exclusions, and useful patterns.

Filed under

,

Published

Written by

Last updated

RHEL root password recovery guide featured image

grep is one of the fastest tools for finding text in Linux logs, configuration files, command output, and scripts. Regular expressions make it more powerful, but a long list of patterns can quickly become hard to read.

This cheat sheet groups the most useful grep and regex examples by task: matching text, extracting values, counting results, searching directories, excluding noise, and combining patterns.

TL;DR

  • Use grep -E for extended regular expressions.
  • Use grep -F when you want literal fixed-string matching.
  • Use grep -o when you only want the matched text, not the whole line.
  • Use grep -r for recursive directory searches.
  • Use grep -v to invert a match and hide noisy lines.

Source check – 1 June 2026: The GNU grep manual documents grep options and regex behaviour. It notes that grep supports Basic Regular Expressions, Extended Regular Expressions, fixed strings, and Perl-compatible regular expressions where available.

Command Index

NeedUseExampleJump
Basic searchgrepgrep 'error' file.logGo
Extended regexgrep -Egrep -E 'error|failed'Go
Only the matchgrep -ogrep -Eo '[0-9]+'Go
Count matchesgrep -cgrep -c '^$'Go
Show context-A, -B, -Cgrep -C 3 'bridge0'Go
Recursive searchgrep -rgrep -r 'TODO' .Go
Exclude linesgrep -vgrep -v '^#'Go
OR / AND logic|, repeated grepgrep 'A' file | grep 'B'Go
Useful patternsRegex snippetsIP, digits, brackets, tabsGo

Grep Modes

Choose the matching mode based on the type of pattern you are using.

grep -G 'pattern' file      # Basic Regular Expression, default mode
grep -E 'a|b|c' file        # Extended Regular Expression
grep -F 'literal.string' file # Fixed string, no regex interpretation
grep -P '\d+' file          # Perl-compatible regex, available in GNU grep builds that support it
grep -r 'pattern' /path     # Recursive search

Practical rule: use -E for most admin regex work, -F for literal strings, and -P only when you specifically need PCRE features such as lookbehind.

grep -i 'foo' filename
grep -w 'target' filename

Match Lines Starting with a String

grep '^S' filename

Return Only the First Match

grep -m 1 'foo' filename

Extract Values

grep -o returns only the matching text. This is useful when you want the value, not the whole log line.

Extract Integers

grep -Eo '[0-9]+' filename

Extract Exactly Three Digits

grep -Eo '[0-9]{3}' filename

Extract IPv4-Looking Addresses

grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' filename
Note

This pattern extracts IPv4-looking strings. It does not validate that each octet is between 0 and 255.

Extract Text Between Two Words

This uses PCRE lookbehind/lookahead, so it needs GNU grep with -P support.

grep -Po '(?<=w1).*(?=w2)' filename

Extract Text Inside Parentheses

grep -Eo '\([^)]*\)' filename

Counting and Line Numbers

Count Empty Lines

grep -c '^$' filename

Count Matching Lines

grep -c 'foo' filename

Count Every Occurrence

grep -o 'foo' filename | wc -l

Show Matching Line Numbers

grep -n 'foo' filename

Context Lines

Context options are useful for logs because the important detail is often before or after the matching line.

grep -A 3 'bridge0' filename  # 3 lines after the match
grep -B 3 'bridge0' filename  # 3 lines before the match
grep -C 3 'bridge0' filename  # 3 lines before and after

Search All Files in a Directory

grep -r 'foo' /path/to/search

Hide Filenames in Multi-File Output

grep -rh 'foo' /path/to/search

Return Only Filenames with Matches

grep -rl 'foo' /path/to/search

Skip a Directory

grep -r --exclude-dir='.git' 'foo' .

Exclude Noise

Return Lines Without a Word

grep -v 'foo' filename

Hide Comment Lines

grep -v '^#' file.txt

Combine Patterns

OR Search

grep -E 'error|failed|timeout' filename

AND Search

grep 'error' filename | grep 'database'

Match Patterns from One File Against Another

grep -Ff patterns.txt target-file.txt

Useful Regex Patterns

NeedPatternExample command
Any single character between A and BA.Bgrep -E 'A.B' file
Optional character, color or colourcolou?rgrep -E 'colou?r' file
Tab character\t or shell quoted tabgrep -P '\t' file
Variable-safe searchQuote the variablegrep "$pattern" "$file"
Known code formatAAEL[0-9]{6}-RAgrep -Eo 'AAEL[0-9]{6}-RA' file
Highlight matches--color=autogrep --color=auto 'foo' file

Common Grep and Regex Mistakes

  • Using * when you mean one or more: [0-9]* can match nothing. Use [0-9]+ with grep -E when you need at least one digit.
  • Forgetting quotes: Quote patterns so the shell does not expand special characters before grep sees them.
  • Assuming grep -P is everywhere: PCRE support depends on the grep build and platform.
  • Using regex for structured data: For JSON, XML, CSV, and logs with strict formats, use a proper parser when accuracy matters.

Related TurboGeek Guides

Final Notes

For RHCSA-style work, focus on practical grep skills: exact string searches, recursive searches, line numbers, context, exclusions, counts, and simple extraction. Advanced regex is useful, but reliable administration usually starts with clear, readable patterns.

7 responses to “Grep and Regex Cheat Sheet for Linux Admins”

  1. […] Part 1 – How to use Grep and Regular Expressions (RegEx) […]

  2. […] Part 1 – How to use Grep and Regular Expressions (RegEx) […]

  3. […] Part 1 – How to use Grep and Regular Expressions (RegEx) […]

  4. […] Part 1 – How to use Grep and Regular Expressions (RegEx) […]

  5. […] Part 1 – How to use Grep and Regular Expressions (RegEx) […]

  6. […] Part 1 – How to use Grep and Regular Expressions (RegEx) […]

  7. […] Part 1 – How to use Grep and Regular Expressions (RegEx) […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Find more on the site

Keep reading by topic.

If this post was useful, the fastest way to keep going is to pick the topic you work in most often.

Want another useful post?

Browse the latest posts, or support TurboGeek if the site saves you time regularly.

Translate ยป