The grep command searches text with patterns on Ubuntu 26.04 LTS and Ubuntu 26.10. This guide expands each example beyond a one-liner so you can filter logs, code and configs with confidence.
Download the Grep Command Cheat Sheet
Grep examples from this guide in one file. Pick PDF for printing, or EPUB / DOCX / MOBI / AZW3 for your reader.
Also see Top 40 Linux commands for Ubuntu 26.04 and advanced Linux commands.
How to use grep on Ubuntu
Basic form: grep [options] pattern file. Patterns can be plain text or extended regular expressions with -E.
Common grep examples
1. Search for a word in a file
Find every line that contains the word error in demo.log.
2. Show line numbers
Add -n so each match prints its line number.
3. Case-insensitive search
Match Error, ERROR and error with a single pattern.

4. Recursive directory search
Walk a tree of files. Start narrowly to keep it fast.
5. Invert match
Print lines that do not contain the pattern.
6. Exact word match
Use -w so error does not also match terror.
7. Multiple patterns
Search for either pattern with -e or extended alternation.
8. Count matches
Show how many matching lines exist per file.
9. Context lines
Print lines before (-B), after (-A) or both (-C) around each match.
10. From a pipe
Filter command output instead of a file.
Useful grep options
| Option | Meaning |
|---|---|
| -i | Ignore case |
| -n | Show line numbers |
| -r / -R | Recurse directories |
| -v | Invert match |
| -w | Whole word |
| -E | Extended regex |
| -c | Count matching lines |
| -C N | Context lines around matches |
Why grep matters
Log triage on Ubuntu almost always starts with grep. Once you can filter with -i, -n and -r, tools like journalctl and tail become much faster to read.
