How to View the End of a File in Linux with the 'Tail' Command

Check out the last few lines with a simple shell utility

There are two very useful commands in Linux that 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.

Example Usage

The tail command by default shows the last 10 lines of a file.

The syntax for the tail command is as follows:


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:

Linux tail command

How to Specify the Number of Lines to Show

Specify the number of lines you want to see using the following command:

sudo tail -n20

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

Linux tail specific length

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
Linux tail starting point

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 | 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
Linux tail bytes

Use the same switch to start showing from a certain byte number as follows:

sudo tail -c+20

How to Monitor a Log File

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

Many scripts and programs don't output to the screen but do append to a log file as they are running. Use the following tail command to check how the log changes every so many seconds:

sudo tail -F -s20

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

sudo tail -F --pid=1234

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

ps -ef | grep

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

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 while 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

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
Was this page helpful?