Commands.page Logo

How to Tail Last 20 Lines and Follow File in Ubuntu

This article provides a direct solution for monitoring file changes in the Ubuntu operating system. It focuses on the specific terminal command required to view the end of a file while keeping the stream open for new data. You will learn the exact syntax to display the last 20 lines and follow subsequent additions in real time.

The command you need is tail. By default, this utility prints the last ten lines of a file to the standard output. To customize this behavior for your specific needs, you must combine two flags: one to set the line count and another to enable following mode.

To show the last 20 lines and follow new additions, run the following command in your terminal:

tail -n 20 -f /path/to/your/file

Replace /path/to/your/file with the actual location of the file you wish to monitor. For example, if you are tracking a system log, you might use sudo tail -n 20 -f /var/log/syslog.

Here is a breakdown of the flags used in this command:

To stop the command and return to the command prompt, press Ctrl + C. This combination sends an interrupt signal to the terminal, safely ending the process. This method is highly effective for debugging applications or monitoring server activity without needing to reopen the file repeatedly.