Viewing files: cat, less, head, tail
How to read any file — tiny config or 2 GB log — from the terminal without opening an editor.
What you'll learn
- Use `cat` for small files and `less` to browse large ones without loading them into memory
- `head` and `tail` slice the top or bottom N lines; `tail -f` follows a live log in real time
- Count lines with `wc -l` and add line numbers with `cat -n` or `less -N`
Before you start
Choosing the right tool
The single question that drives the choice: how big is the file, and is it still being written?
| Situation | Tool |
|---|---|
| Small file, want to see it all | cat |
| Large file, want to scroll around | less |
| Just the first few lines | head |
| Just the last few lines | tail |
| File is growing live (a server log) | tail -f |
cat — dump a file to the screen
cat (short for concatenate) reads one or more files and writes their contents to stdout — the terminal’s standard output stream.
cat notes.txt
Meeting notes — 2026-06-05
Attendees: Priya, Arjun, Leela
Action items:
1. Deploy hotfix by Friday
2. Update the runbook
You can concatenate two files into a third:
cat part1.txt part2.txt > combined.txt
Line numbers with cat -n
cat -n config.ini
1 [database]
2 host = localhost
3 port = 5432
4 name = appdb
less — the pager
A pager is a program that shows one screenful of text at a time and waits for your keypress. less is the standard pager on Linux and macOS. It never loads the whole file into memory, so it opens a 2 GB log instantly.
less /var/log/app/production.log
Key controls inside less:
| Key | Action |
|---|---|
Space / f | One page forward |
b | One page back |
g | Jump to top |
G | Jump to bottom |
/pattern | Search forward (press n for next match) |
q | Quit |
Show line numbers inside less:
less -N /var/log/app/production.log
1 2026-06-05 00:01:14 INFO Server started on :8080
2 2026-06-05 00:01:15 INFO Database connected
3 2026-06-05 00:03:44 ERROR Timeout on /api/orders (upstream=payments)
head — the first N lines
head prints the first 10 lines by default. Use -n to change that.
head server.log
2026-06-04 23:58:01 INFO Server started on :8080
2026-06-04 23:58:02 INFO Database connected
2026-06-04 23:58:02 INFO Cache warmed (12 048 keys)
2026-06-04 23:58:10 INFO First request received
...
head -n 3 server.log
2026-06-04 23:58:01 INFO Server started on :8080
2026-06-04 23:58:02 INFO Database connected
2026-06-04 23:58:02 INFO Cache warmed (12 048 keys)
tail — the last N lines
tail prints the last 10 lines by default. Same -n flag.
tail -n 5 server.log
2026-06-05 02:14:09 ERROR Timeout on /api/orders (upstream=payments)
2026-06-05 02:14:10 ERROR Timeout on /api/orders (upstream=payments)
2026-06-05 02:14:11 WARN Circuit breaker opened for payments service
2026-06-05 02:14:12 INFO Returning cached response for /api/orders
2026-06-05 02:14:15 INFO Circuit breaker half-open, probing payments
tail -f — follow a live log
The -f flag (follow) keeps tail running and prints new lines as they are appended to the file. This is the killer feature for watching a server while you reproduce a bug.
tail -f /var/log/app/production.log
2026-06-05 02:20:01 INFO GET /healthz 200 (2ms)
2026-06-05 02:20:05 INFO POST /api/checkout 200 (84ms)
2026-06-05 02:20:07 ERROR POST /api/checkout 500 — NullPointerException in CartService
Lines stream in as they arrive. Press Ctrl+C to stop.
Combine -f with -n to also show the last N lines on startup:
tail -n 20 -f /var/log/app/production.log
wc -l — count lines
Before deciding which tool to use, a quick line count is useful context.
wc -l server.log
4 238 492 server.log
Four million lines — definitely reach for less or tail, not cat.
Quick-reference diagram
Summary
cat file— print a small file;cat -nadds line numbers.less file— browse any size;/patternto search,qto quit,less -Nfor line numbers.head -n N file— first N lines (default 10).tail -n N file— last N lines (default 10).tail -f file— stream new lines as they arrive;Ctrl+Cto stop.wc -l file— count lines before deciding which tool to reach for.