Skip to content

Linux Foundations / Find your way around Linux ยท Step 39 of 63

Lesson: tailSwitch
โ† Linux Foundations

Shell and files

tail

Learn to read the end of files and follow logs as they grow with tail.

The newest information in a log is at the bottom. tail shows the end of a file, and it can keep watching as new lines arrive.

The last lines

By default you get the last 10 lines. -n chooses another number.

$ seq 100 | tail -n 3
98
99
100

Start from a given line

A plus sign changes the meaning: -n +N prints from line N through to the end.

$ printf 'name\nmaya\nomar\n' | tail -n +2
maya
omar

This is a common way to skip a header row. Note that +2 starts at line 2, so only one line is skipped.

As with head, -c counts bytes instead of lines.

Follow a growing file

-f prints the end of the file and then waits, showing new lines as they are written.

$ tail -f app.log
2026-03-01 10:00:01 service started
2026-03-01 10:00:09 request received

Press Ctrl-C to stop. You may need sudo to read system logs, and their location differs between distributions. Systems using systemd keep much of the log in the journal, which you follow with journalctl -f.

Rotated logs

Logs are often rotated: the current file is renamed and a new one takes its place. tail -f keeps following the original file, even under its new name. tail -F follows the name and retries, so it picks up the new file. -F is available in GNU and BSD tail.

Combine head and tail

To print a slice from the middle, chain them. This shows lines 4 to 6:

$ seq 10 | head -n 6 | tail -n 3
4
5
6

You can also filter a live log. tail -f app.log | grep error shows only matching lines as they arrive.

Practise reading files in the streams and text lab.

Try it

  1. Run seq 20 | tail -n 5.
  2. Run seq 5 | tail -n +3.
  3. In one terminal, run touch demo.log and tail -f demo.log.
  4. In another terminal, run echo hello >> demo.log and watch the first one.

Check your understanding

tail

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.