How To View The End Of A File In Linux With The Tail Command

Linux logo with penguin head


There are two very useful commands in Linux which let you see part of a file. The first is called head and by default, it shows you the first 10 lines in a file. The second is the tail command which by default lets you view the last 10 lines in a file.

Why would you want to use either of these commands? Why not just use the cat command to view the entire file or use an editor such as nano?

Imagine the file you are reading has 300,000 lines in it. Imagine also that the file consumes a lot of disk space.

A common use for the head command is to make sure that the file you want to view is indeed the correct file. You can usually tell if you are looking at the correct file just by seeing the first few lines. You can then choose to use an editor such as nano to edit the file.

The tail command is useful for viewing the last few lines of files and is very good when you want to see what is happening in a log file held in the /var/log folder.

This guide will show you how to use the tail command including all of the available switches.

Example Usage

As mentioned previously, the tail command by default shows the last 10 lines of a file.

The syntax for the tail command is as follows:

tail <filename>

For example, to view the boot log for your system, you can use the following command:

sudo tail /var/log/boot.log

The output would be something like this:

 * Starting enable remaining boot-time encrypted block devices [ OK ]
* Starting save udev log and update rules                 [ OK ]
* Stopping save udev log and update rules                [ OK ]
* speech-dispatcher disabled; edit /etc/default/speech-dispatcher
* VirtualBox Additions disabled, not in a Virtual Machine
saned disabled; edit /etc/default/saned
* Restoring resolver state...                            [ OK ]
* Stopping System V runlevel compatibility                [ OK ]
* Starting MDM Display Manager                        [ OK ]
* Stopping Send an event to indicate plymouth is up         [ OK ]

How to Specify the Number of Lines to Show

Maybe you want to see more than the last 10 lines of the file. You can specify the number of lines you want to see using the following command:

sudo tail -n20 <filename>

The above example would show the last 20 lines of the file.

Alternatively, you can use the -n switch to specify the starting point in the file as well. Perhaps you know the first 30 rows in a file are comments and you just want to see the data within a file. In this case, you would use the following command:

sudo tail -n+20 <filename>

The tail command is often used alongside the more command so that you can read the file a page at a time.

For example:

sudo tail -n+20 <filename> | more

The above command sends the last 20 lines from filename and pipes it as the input to the more command:

You can also use the tail command to show a certain number of bytes instead of lines:

sudo tail -c20 <filename>

Again you can use the same switch to start showing from a certain byte number as follows:

sudo tail -c+20 <filename>

How to Monitor a Log File

There are many scripts and programs that don't output to the screen but do append to a log file as they are running.

In this instance, you might want to monitor the log file as it changes. You can use the following tail command to check how the log changes every so many seconds:

sudo tail -F -s20 <filename>

You can also use tail to continue monitoring a log until a process dies as follows:

sudo tail -F --pid=1234 <filename>

To find the process id for a process you can use the following command:

ps -ef | grep <programname>

For example, imagine you are editing a file using nano. You can find the process ID for nano using the following command:

ps -ef | grep nano

The output from the command will give you a process ID. Imagine the process ID is 1234.

You can now run tail against the file being edited by nano using the following command:

sudo tail -F --pid=1234 <filename>

Every time the file is saved within nano, the tail command will pick up the new lines at the bottom. The command only stops when the nano editor is closed.

How to Retry the tail Command

If you receive an error whilst trying to run the tail command because it is inaccessible for some reason then you can use the retry parameter to keep retrying until the file is available.

sudo tail --retry -F <filename>

This only really works in conjunction with the -F switch, as you need to be following the file to want to retry.


To find out more information about the tail command you can use the following command:

man tail

You will notice that we have included sudo within most of the commands. This is only necessary where you don't have permissions as your normal user to view the file and you need elevated permissions.