How to use tail command in Linux? A Detailed tutorial!


Linux tail command prints the first given number of lines from the bottom of a file by default it is 10 lines. It is complementary to the Linux head command which prints the first given number of lines from the start of a file.

In this article, you will see the usage of the Linux tail command along with some examples.

How to use the tail command in Linux

The syntax of how to use a tail command on a Linux system is given below.

tail [option].. [file]

Where you can find a list of options available to use with the tail command on the man page of tail command. Like the head command, you can enter one or more files as input to this command.

Examples of Linux tail command

Let’s say we have a file distro.txt, you can see the content of this file which is given below.

Now when you run the tail command by default it will print the 10 lines from the bottom of the given file.

tail filename.txt

For example –

tail distro.txt

Here last 10 lines of the file distro.txt will get printed.

How to display a specific number of lines

To display a specific number of lines of a file you need to use option -n or --lines with the tail command. For example to display the last 5 lines of file distro.txt use –

tail -n 5 distro.txt

alternatively, you can use –

tail -5 distro.txt

You can see the output of this command in the given image –

How to display a specific number of bytes

You can display the specified number of bytes of a file, you need to use the option -c or --bytes with the tail command. For example to display the last 50 bytes of the file distro.txt use –

tail -c 50 distro.txt

Now see the output in the given image –

How to display the content of multiple files

You can provide more than one file as input to the tail command by default it will display the last 10 lines of each file. For example, if there are two files then it will print the last 10 lines from the first file and the last 10 lines from the second file.

For example –

head file1.txt file2.txt

Similarly, you can use the other available options with the tail command. This command can be used with other commands using pipes. For more help, you could run –

man tail

Conclusion

Ok, that’s all for now. if you have any query then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.