Reading Files — Terminal

cat prints a whole file (right for short files, -n adds line numbers). less opens a file page by page: Space goes forward, b back, / followed by a text (for…

A server (the program that runs a website) records every event in a log file, one line per event. That log file is like the paper roll in a cashier's receipt printer: every new sale is printed at the bottom end, and the roll grows longer every hour. Each receipt on the roll is one line of the log. You rarely need the whole roll. There are five ways to read it:

All of that works from the terminal. You see the file without opening an editor (an app for writing text), and you choose which part you want to see.

The examples below run in Budi's folder ~/web-project/logs. In that folder is app.log, the log of the server of Budi's cake shop. It has 24 lines. Each line has a date, a time, a level (how important it is: INFO, WARN, ERROR) and what happened.

cat: show everything

Budi is in the folder logs. To read style.css in its parent folder, he writes the path ../style.css:

budi@laptop:~/web-project/logs$ cat ../style.css
body {
  font-family: sans-serif;
}
h1 {
  color: brown;
}

less: read page by page

less app.log opens the file full screen, one page at a time, and waits for you to press a key:

Key What it does
Space or f Next page
b Previous page
g / G To the start / to the end of the file
/ERROR then Enter Searches down for ERROR
n Next search result
q Quit

head and tail: the start and the end

These are the first 3 lines and the last 3 lines of app.log:

budi@laptop:~/web-project/logs$ head -n 3 app.log
2026-10-08 09:00:01 INFO server started on port 3000
2026-10-08 09:00:04 INFO GET / 200
2026-10-08 09:00:04 INFO GET /style.css 200
budi@laptop:~/web-project/logs$ tail -n 3 app.log
2026-10-08 09:09:40 INFO POST /pay 200
2026-10-08 09:10:05 INFO GET /thank-you 200
2026-10-08 09:10:30 INFO GET / 200

tail -f: watch a file that keeps being written

This is tail -f left running for a while, then stopped. The ... marks lines this example doesn't show:

budi@laptop:~/web-project/logs$ tail -f app.log
2026-10-08 09:05:11 INFO GET / 200
...
2026-10-08 09:10:30 INFO GET / 200
2026-10-08 09:11:02 INFO GET /products 200
^C
budi@laptop:~/web-project/logs$

wc: count lines, words and bytes

This is wc on app.log, once with no flag and once with -l:

budi@laptop:~/web-project/logs$ wc app.log
  24  148 1060 app.log
budi@laptop:~/web-project/logs$ wc -l app.log
24 app.log

⚠️ When the screen floods or you can't get out

TL;DR: cat prints a whole file (right for short files, -n adds line numbers). less opens a file page by page: Space goes forward, b back, / followed by a text (for example /search) searches for that text, n goes to the next result, and q quits. head takes the start of a file and tail the end (without -n, 10 lines, and -n 3 for 3 lines). tail -f keeps showing new lines until you press Ctrl+C. wc counts lines, words and bytes (wc -l only lines). In a log (a file where a program records events) the newest part is at the bottom, so tail is the one you'll use most.

⚡ A short file: cat

Write:

cat ../style.css

Meaning: cat prints a whole file without stopping, here style.css in the parent folder (..). Right for short files. If you open a log (a file where a program records events) thousands of lines long with cat, the screen floods: the first lines scroll past before you can read them.

⚡ A long file: less

Guess first: the log is thousands of lines, and you want to read it slowly, scrolling up and down. Which command?

less. It opens the file page by page: Space goes forward, b back, /ERROR searches for the text ERROR, and q quits.

Meaning: less only borrows the screen. After q, the file's content disappears from the screen and the prompt comes back as it was.

⚡ The start and the end

Write:

head -n 3 app.log
tail -n 3 app.log

Meaning: app.log is the log of the server (the program that runs the website) of Budi's cake shop. head takes lines from the top of the file, tail from the bottom, and -n 3 means 3 lines. Without -n, both take 10 lines. In a log, the newest event is at the very bottom.

⚡ Keep watching

Write:

tail -f app.log

Meaning: -f (follow) makes tail not finish: every new line the server writes into app.log appears right away. Stop it with Ctrl+C.

What you will learn