AI Hints
Home›Linux Tutorial›Reading Files
Linux · Lesson 32 of 120

head and tail

The start and the end.

⏱ 2 min read▶ Video at 39:52🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

Log files can grow to millions of lines. Most of the time, you only care about the start, or the most recent lines at the end. Head and tail show exactly those.

head and tail
From the video: head and tail
  • Logs grow huge
  • You want the start, or the end
  • head and tail

The first lines

Here's a web server log, with sixty lines. Head shows the first ten lines, by default. Dash N, and a number, picks how many. Head dash N three shows just the first three.

Terminal
wc -l logs/server.log
60 logs/server.log
head logs/server.log
2026-01-05 09:00:23 ERROR priya failed /home
2026-01-05 09:00:29 INFO sam opened /home
2026-01-05 09:00:55 INFO sam opened /about
2026-01-05 09:01:00 INFO priya opened /home
2026-01-05 09:01:18 INFO priya opened /home
2026-01-05 09:01:57 INFO alex opened /home
2026-01-05 09:02:40 INFO sam opened /login
2026-01-05 09:03:20 INFO alex opened /home
2026-01-05 09:03:58 WARN jo slow page /cart
2026-01-05 09:04:10 INFO sam opened /shop
head -n 3 logs/server.log
2026-01-05 09:00:23 ERROR priya failed /home
2026-01-05 09:00:29 INFO sam opened /home
2026-01-05 09:00:55 INFO sam opened /about

The last lines

Tail does the opposite. It shows the last lines, the newest entries in a log. Tail dash N four shows the last four. And tail dash N two shows only the last two. These are the most recent things that happened on our server.

Terminal
tail -n 4 logs/server.log
2026-01-05 09:25:38 INFO sam opened /about
2026-01-05 09:26:14 INFO alex opened /login
2026-01-05 09:26:18 WARN jo slow page /home
2026-01-05 09:26:26 INFO jo opened /login
tail -n 2 logs/server.log
2026-01-05 09:26:18 WARN jo slow page /home
2026-01-05 09:26:26 INFO jo opened /login

Watching a log live

Tail has one more superpower. Tail dash F, for follow, keeps the file open, and prints new lines as soon as they're added. People use it to watch a server log live, while they test a website. Press control C to stop following.

keyvalue
tail -f filefollow new lines live
Used forwatching logs
Stop withCtrl + C
head -n / tail -nchoose how many lines

Summary

Head shows the start of a file, tail shows the end, and dash N picks how many lines. Tail dash F follows a log live. Next, less, for reading long files comfortably.

Key takeaways
  • head -n 5 file
  • tail -n 5 file
  • tail -f follows
Next up: less and wc — reading and counting