Save 10%Pay yearly on any plan. 6% half-yearly, 3% quarterly. Help centreWhatsAppTelegram TeamsOpen a ticket

How to Use the tail Command in Linux

Show the last lines of a file with tail, follow a log as it grows with -f, survive log rotation with -F, and combine tail with grep and head.

2 min readBy Smita

Reviewed by the AmazingRDP technical team

The tail command in Linux

Logs are where a server tells you what is wrong. A Linux VPS gives you root access to read them all. See Linux VPS from $6.99

tail prints the end of a file. That sounds modest, and it is one of the most used commands on any server, because the newest lines of a log file are at the end, and the newest lines are what you want when something has just gone wrong.

The basics

tail /var/log/syslog
tail -n 50 /var/log/syslog
tail -n +100 notes.txt
tail -c 200 data.bin
FormShows
tail fileThe last 10 lines
tail -n 50 fileThe last 50 lines
tail -n +100 fileEverything from line 100 to the end
tail -c 200 fileThe last 200 bytes

Many log files are readable only by root, so put sudo in front where needed.

Follow a file as it grows

tail -f /var/log/nginx/access.log

-f keeps the command running and prints each new line the moment it is written. Reload the web page, and the request appears. Stop with Ctrl + C.

-F for logs that rotate

tail -F /var/log/nginx/error.log

Logs are regularly renamed and started afresh ("rotated"). Plain -f keeps watching the old, renamed file and falls silent. Capital -F follows the name and picks up the new file. For anything you watch for more than a few minutes, use -F.

Several files at once

tail -F /var/log/nginx/access.log /var/log/nginx/error.log

Each block is headed by the file name. -q hides those headers.

Only the lines you care about

tail -F /var/log/auth.log | grep --line-buffered "Failed password"
tail -n 1000 app.log | grep -i error
tail -n 1000 app.log | grep -c timeout

The first watches live for failed logins; --line-buffered makes grep pass each line on at once. The second searches the last thousand lines; the third counts matches.

A slice from the middle

head -n 120 file.txt | tail -n 21

head is the opposite of tail: it prints the beginning. Together they cut out lines 100 to 120.

Relatives worth knowing

CommandUse
less +F fileFollows like tail -f; press Ctrl + C to scroll back and search, F to follow again
journalctl -fFollows the system journal, where many services log today
journalctl -u nginx -n 50 -fThe last 50 lines of one service, then follow
dmesg -wFollows kernel messages

If a service has no file under /var/log, its messages are in the journal.

Everyday uses

  • Why did the service not start? sudo journalctl -u servicename -n 50.
  • Who is trying to log in? sudo tail -F /var/log/auth.log (on the RHEL family: /var/log/secure).
  • Is the script still running? tail -f job.log on the file it writes to: see how to run a shell script.
  • What did the last cron run say? See scheduling tasks with cron.

Good to know

  • tail does not load the whole file; it is quick even on files of many gigabytes.
  • On Windows, PowerShell has the same idea: Get-Content file.log -Tail 50 -Wait.

For a server whose logs you can read freely, see our Linux VPS plans. Related: list open ports on Linux.

Questions

How do I see the last lines of a file in Linux?

Run tail file for the last 10 lines, or tail -n 50 file for the last 50.

What does tail -f do?

It keeps running and prints new lines as they are added to the file. Stop it with Ctrl + C.

What is the difference between tail -f and tail -F?

-F keeps following when the file is renamed and recreated, as happens when logs rotate. -f does not.

How do I follow a log and filter it?

Pipe it into grep: tail -F file | grep --line-buffered word.