tail Command in Linux Explained [With Examples]

Written by: Linuxopsys   |   Last updated: July 10, 2023

When talking about viewing files in Linux, common commands that come to our picture are cat, more, less, head, and tail. Each command has its own unique features where it standout. Here we are going to learn about tail command in Linux.

The tail command is a powerful tool used in Linux, primarily used to view the end of files. This comes useful to track growing files such as log files. By default tail display the last 10 lines to the standard output.

So tail would be the best tool to check the most recent entries in a file.

Syntax and Options

The basic syntax of Linux tail command:

tail [OPTION]... [FILE]...


  • [FILE]... : Specific file or files you want to view the end of.
  • [OPTIONS]... : This changes the behavior of the command. Let's look at some of the commonly used options:
    • -n : Specific number of last lines to print.
    • -f or --follow : Follow the file. ie Display added data of the file.
    • -F or --follow=name --retry : Same follow file same time watch for the file if it's inaccessible ( for example file got deleted).
    • q : Useful when viewing multiple files to remove headers.
    • v : For a single file this print the name of the file in the first line of the output.
    • -s or --sleep-interval=N : This is used with -f option to specify the time interval to check for new lines.
    • -c : Display the last specified number of K bytes.

Practical Usage of tail Command

Let's look into detail how we can use practical use tail for viewing and monitoring.

Viewing the End of a Text File

One purpose of tail command is to view and manipulate the number of lines at the end of the file.

To illustrate we have a sample file with 11 lines.

cat country.txt


tail country.txt
view a file using tail command

This command prints the last 10 lines of the file named country.txt. As mentioned before this is the default behavior of the tail command.

You can use -n to manipulate this default value.

tail -n 4 country.txt
tail -4 country.txt
print the last 4 line of a file using tail

This print the last 4 number of lines of the file named country.txt

One practical example is where you need to print the last 100 lines from /var/log/syslog file:

tail -n 100 /var/log/syslog

Using + operator you can specify the starting line number where tail command needs to start reading.


tail +5 number.txt
tail -n +5 number.txt 
print line starting from line number 5 using tail

This command print from line number 5 to till end of the file.

You can use -c option ie Byte Offset which allows specifying the number of bytes instead of lines.

tail -c -6 country.txt
tail -c 6 country.txt 
print last 6 bytes using tail command

This command display the last 6 bytes of the file named country.txt.

With the positive number, it prints file content after skipping that number of bytes from the start of the file.

tail -c +6 country.txt 
print all line skipping the first 6 bytes

Tail -f

The tail -f command is used to continuously monitor a file to display newly added data of the file. This comes more useful for log file monitoring. You can watch each new entries in real-time.


tail -f /var/log/syslog
tail print last 10 lines and wait for new entries in the file

This command print the last 10 lines of the file /var/log/syslog and then tail will wait for the new entries.

The tail -f will keep watching for new entries unless you press Ctrl+C to stop it. On this tail -f will clear the screen and you won't be able to navigate or see the data on the terminal. Then the only option see the data would be to open the file with any file content viewer. Alternatively, you can use less +F.

Using tail -F for Persistent File Tracking

The tail -f won't be able to watch new entries if the file is replaced or deleted. That means tail -f follows the file descriptor. The fail -F (or --follow=name --retry) follows the filename. That means even if you replace or delete the file it wait for the data.


tail -F /var/log/syslog

This command performs a persistent monitoring of /var/log/syslog file, even if this recreated tail will watch new entries. This comes useful for logs especially when it has log rotations.

Multiple Files

The tail command allows multiple files to be passed as arguments to it. This will list each file's last 10 lines. There will be a header before each file entry showing its filename.

tail food.txt country.txt 
viewing multiple files using tail

You can use -q option if you want to remove headers, then look more neat.

tail -q food.txt country.txt
hide headers when viewing multiple files

This prints the last 10 lines of both files without displaying the filenames.

Multiple files come most useful when using tail -f. This way you can monitor multiples files in real time.


sudo tail -f -n 5 /var/log/dmesg /var/log/syslog
viewing multiple files and waiting for new entries using tail -f

This command prints the last 5 lines from /var/log/dmesg and /var/log/syslog file and then continuously waits for new entries in both files.

Adjusting the Sleep Interval with -s

The -s option only applies for tail -f or tail -F command. When we run tail -f by default the command check for new entries every one second. We can adjust this interval using the -s option.


tail -f -s 5 /var/log/dmesg

This command checks every 5 seconds for new lines in /var/log/dmesg file. This helps to check files that don't have updates very often.

Combining tail with Other Linux Commands

The tail command can be combined with other Linux commands to archive complex tasks. Mostly it is combined with commands such as grep, awk, or head.

With grep:

tail -n 100 /var/log/syslog | grep failed
tail with grep

This find lines containing the pattern 'failed' in the last 100 lines of the file /var/log/syslog.

With sort:

tail –n 5 country.txt | sort
tails with sort command

This prints the last 5 lines and sort them alphabetically from the country.txt file

With head:

tail –n +5 country.txt | head –n 2
tail with head command

This command first gets all lines starting from line 5 from the country.txt file and then prints the first two lines from those.



Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment