Grep Command on Ubuntu 26.04 (2026): Examples + Cheat Sheet

Share

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.

Before You Start
Ubuntu version
Works the same on Ubuntu 26.04 LTS and 26.10. GNU grep is installed by default.
Terminal
Open with Ctrl + Alt + T. Pipe long output through less when needed.
Practice file
Create a tiny log with printf 'error\ninfo\nERROR\n' > demo.log before trying the samples.
KEY TAKEAWAYS
01Pattern search first
grep finds lines that match a string or regular expression inside files or pipes.
02Flags you will reuse
-i, -n, -r, -v and -E cover most daily Ubuntu troubleshooting.
03Cheat sheet included
Download the free grep cheat sheet in PDF or ebook formats.
04Combine with find
find locates files; grep searches inside them. Use both for large trees.
05Mind binary files
Add -a or –binary-files=text carefully; prefer text logs and source files.
06Related reading
Pair with the Top 40 commands guide and the advanced commands list.
Free Cheat Sheet

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.

grep –help | head
grep --help | head

Common grep examples

1. Search for a word in a file

Find every line that contains the word error in demo.log.

grep "error" demo.log
grep "error" demo.log

2. Show line numbers

Add -n so each match prints its line number.

grep -n "error" demo.log
grep -n "error" demo.log

3. Case-insensitive search

Match Error, ERROR and error with a single pattern.

grep -i "error" demo.log
grep -i "error" demo.log
Sample terminal output of grep -n -i error on a demo log file
Sample output: grep -n -i error on a demo log (generated for this guide).

4. Recursive directory search

Walk a tree of files. Start narrowly to keep it fast.

grep -RIn "TODO" ~/projects
grep -RIn "TODO" ~/projects

5. Invert match

Print lines that do not contain the pattern.

grep -v "info" demo.log
grep -v "info" demo.log

6. Exact word match

Use -w so error does not also match terror.

grep -nw "error" demo.log
grep -nw "error" demo.log

7. Multiple patterns

Search for either pattern with -e or extended alternation.

grep -e "error" -e "warn" demo.log
grep -e "error" -e "warn" demo.log
grep -E "error|warn" demo.log

8. Count matches

Show how many matching lines exist per file.

grep -c -i "error" demo.log
grep -c -i "error" demo.log

9. Context lines

Print lines before (-B), after (-A) or both (-C) around each match.

grep -n -C 2 "error" demo.log
grep -n -C 2 "error" demo.log

10. From a pipe

Filter command output instead of a file.

journalctl -u ssh –no-pager | grep -i failed
journalctl -u ssh --no-pager | grep -i failed

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.