Tail Command in Linux – Options +Example

Last updated: August 16, 2022

In a Linux system, log data are normally added to the end of the file. You may require to see the new additions to the log file. Linux tail command is commonly used in this scenario to view those data.

In this tutorial, we learn about the Linux tail command and its useful options.

What is tail command 

Linux tail command is a complementary part of head command. It prints the last ‘N’ number of lines from one or more files. By default, it prints the last 10 lines of a provided file.

Usually, in the data files such as error logs, access logs the data is added to the end of the file. The tail command helps to read last lines and monitor file changes in real-time.

How to use tail command 

The basic syntax of Linux tail command:

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

For example, we have created a text file named ‘country.txt’. In this file, we copied the names of different countries in the world. To view the file content we may use cat command:

cat country.txt
output of cat filename

Display the same file data using the tail command.

tail country.txt

When we use the tail command without any option, it prints only the last 10 lines of a file. 

output of tail command

Tail Command Examples

Here we will check some of the commonly used tail commands with practical examples.

Print the specified last lines

Using the tail command, we can print the specified last lines of a file. To print the last 4 number of lines from a specified text file:

tail -n 4 country.txt

or

tail -4 country.txt
tail specific number of last lines

One practical example, to check the last 100 lines from /var/log/syslog file, type:

tail -n 100 /var/log/syslog

Prints lines from start of a specific line

Using the ‘+’ option with a number, we can display the file content from the start of a specified line number till the end. For example to print all contents of the file named country.txt from line 1, type:

tail +1 country.txt

or

tail -n +1 country.txt 
tail print from specific line

The following command prints file contents from line 4:

tail +4 country.txt
print from line 4

This comes helpful when you have a large file and want to print from a particular line number.

Print last specified bytes

Tail command is useful to display the last specified number of characters or bytes from a file. For example, to print the last 6 characters or bytes from a file named country.txt, type:

tail -c -6 country.txt

or

tail -c 6 country.txt 
print last specified bytes

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

tail -c +6 country.txt 
skip specific bytes from start

tail command and multiple files

You can use multiple files with tail command. By default, if you pass multiple files it prints the file names in between.

tail food.txt country.txt 
tail multiple files

To ignore filenames, use -q option when using more than one file.

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

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

Monitor files real-time

Monitoring the new text entries especially logs files is one of the best use of the tail command. Use the tail command followed by f option to track files. By default it tail -f command shows the last 10 lines and the console will show the new lines when the file gets updated.

For example to track /var/log/syslog file in real-time, type:

tail -f /var/log/syslog
tail -f command

You need to press the interrupt key to return back to the command prompt.

Use tail command with other commands

We can also use the Linux tail command in conjunction with other Linux commands. For example, you can use the sort command piped with the tail command:

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

This prints the last 5 lines and sort them alphabetically. 

You may also use the tail command along with head command:

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

This is another useful example where we can combine tail command along with nl command:

nl coountry.txt | tail -7
nl with tail command

This prints the last specified number of lines with line numbers:

Tail Commands Options

Some of the useful options of tail command are:

OptionsDescription
-n NumPrints the last Num lines.
-c NumPrint the last Num bytes
-q Hide the headers of a given file names
-fDisplay appended data as the file grows
-vPrints the specified file names at the beginning of the output
--versionPrints the installed version information

Conclusion 

In this tutorial, we learned the basic functionality of tail command and how it can be used to show the contents from the end of a file. Tail command has very few options which we have explained with examples.

SHARE

Comments

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

Leave a Reply

Leave a Comment