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:
- unroll the whole thing (
cat), - move through it slowly while you look for something (
less), - read the first few receipts (
head), - read the newest ones at the end (
tail), - stand by the printer and read each receipt as it comes out (
tail -f).
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;
}
cat fileprints the whole file without stopping. It suits a short file likestyle.css, only 6 lines.cat -n fileprints line numbers too.- For a long file, this is a problem: the first lines scroll off the top of the screen before you can read them. That's what
less,headandtailare for.
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 |
- The last line on the screen is the status: the file name when it has just opened,
:while you scroll, and(END)at the end of the file. - After
q, the file's content disappears from the screen and the prompt comes back as it was.lessonly borrows the screen until you quit, while the output ofcatstays written on the screen.
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
headtakes lines from the top of the file,tailfrom the bottom. Without-n, both take 10 lines.-n 3means 3 lines. The short form is-3:tail -3 app.log.- In a log the newest line is at the very bottom. So the 10 newest events come from
tail -n 10 app.log: lines 15 to 24.
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$
-f(follow) prints the last 10 lines, then doesn't finish:tailwaits, and every new line the server writes appears right away. In the example, the09:11:02line appears on its own when a visitor opens the product page.- Stop it with
Ctrl+C. The terminal writes^C, and the prompt comes back. - This is an everyday developer habit: the server runs in one terminal window, and
tail -fshows its log in another terminal window.
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
wc(word count) prints three numbers: the number of lines, words and bytes, then the file name.-lonly lines,-wonly words,-conly bytes.
⚠️ When the screen floods or you can't get out
caton a big file. A log of thousands of lines floods the screen right away. Uselessto read it, andtailfor the newest part.- Getting stuck in less or tail -f. Leave
lesswithq. Stoptail -fwithCtrl+C. - Thinking head and tail show everything. Without
-n, only 10 lines come out. caton a folder.cat logsin the folderweb-projectanswerscat: logs: Is a directory(it is a folder). What you read has to be a file.
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.