datarekha

Viewing files: cat, less, head, tail

How to read any file — tiny config or 2 GB log — from the terminal without opening an editor.

6 min read Beginner Command Line Lesson 4 of 14

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?

SituationTool
Small file, want to see it allcat
Large file, want to scroll aroundless
Just the first few lineshead
Just the last few linestail
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:

KeyAction
Space / fOne page forward
bOne page back
gJump to top
GJump to bottom
/patternSearch forward (press n for next match)
qQuit

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

Is the file live (growing)?NoYes →tail -fFile size?LargelessSmallcat / head / tail
Pick the tool by file size and whether it is still being written

Summary

  • cat file — print a small file; cat -n adds line numbers.
  • less file — browse any size; /pattern to search, q to quit, less -N for 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+C to stop.
  • wc -l file — count lines before deciding which tool to reach for.

Quick check

0/3
Q1You open `less server.log` and want to jump straight to the end of the file. Which key do you press?
Q2A colleague shares a 3 GB CSV export and asks you to check its column headers. Which command shows only the first line?
Q3Your team just deployed a new service. You run `tail -f app.log` and watch traffic come in. Suddenly the log goes silent — no new lines for 30 seconds. What is the most likely cause?

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

Explore further

Cheat sheets

Related lessons

Skip to content