VPS

Linux Tail Command: What It is and How to Use It

Many advanced Linux users use commands that print certain parts of files. There are head and tail commands which define the beginning and end of a file or command.

Tail commands allow the user to read the end commands of a file. It is also helpful in monitoring new information updated in real time in a specific file. This makes verifying the latest entries in the system easy. Users can also view multiple files using a tail command in Linux.

Let’s begin with the tail command and explore all the elements that this simple and easy command can provide.

What is a Tail Command?

The Tail command is a great command used to print the last N numbers or tails of an input. Usually, it displays, or prints, the last 10 numbers of the file given to it via standard input and delivers the result in the standard output. If we use it over a single filename, data from each file proceeds by its filename with a header.

There are around 14 important tail commands in Linux which serves different purposes for the users. –n is used to output the last number lines in a file instead of the default 10 lines.

How to use the Tail Command?

Remember, before using the command you’ll have to use the Terminal or PuTTY to SSH into your VPS server. Here’s a tutorial detailing the process.

By default, Tail prints the last 10 lines of a file in Linux operating system and then ends.

To view the last ten numbers of a file write the below command:

tail name_of_file

Or alternatively:

tail file_path

The output will display the last 10 lines of the file.

How to Limit the Number of Lines Shown?

Tale provides the users the ability to view the number of lines as per the requirement by using –n command.

The simple and easy way to perform this is:

Tail –n* file_path

The * indicates the number of lines you wish to print.

For Example, if we wish to view two lines from a particular file, we will use the below command:

tail -n2 devisers.txt

2 is the number of file to view, while devisers.txt defines the name of the file.

How to View the Last Number of Bytes of Data?

To view the last number or certain number of bytes of a very large file, use the -c option to simplify the tail to view.

tail –c* file_path

Here the * represents the number of bytes. Replace it accordingly to what you wish to see.

How to Watch a File for Changes?

To examine a file for variations use the tail command followed by the -f option. This command is helpful when viewing the last ten lines of a file. This option is commonly used to track log files in real time. For our devisers.txt file the command would look like this:

tail -f devisers.txt

Newer versions of tail commands also let the user watch multiple files or folders. Once the original file changes, the header will show which line someone made the changes to.

How to Use Tail Cmmand with Other Linux Commands?

Tail commands can be piped with other Linux command line features. Here, we are going to use tail command with:

Using the Tail Command with -r (Reverse Order)

Here, we want to view the output of the tail command in reverse order.

tail –n* file_name | sort -r

The first part of the command is as we used it before, afterwards we separate it with | to indicate the follow up – sort -r, that will – as the name suggests – sort the output in reverse order.

Using the Tail Command with ls (Oldest File or Folder)

With the help of the following example we will view the seven files or folders changed the longest time ago.

To view this, output of the ls command pipes to tail command. It would have to look like this:

ls -l | tail -n8

As previously, in the tail -n8 part of the command you can insert your prefered number.

Summary

Tail is an incredibly flexible command that can improve the management of your files greatly. You only need to specify the files and options and use the command in the correct syntax.

Once you get the hold of the language and understand the common uses of the command, you can modify it in a number of ways for developing applications, software and more.

M. Aitchison, the developer of the FreeDos Version, has elaborated the use and mediums for which the tail command is best suited. Develop modern applications that can be maneuvered error-free and with ease.

About the author

Edward S.

Edward is Hostinger's copywriter. He's an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward's goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Add Comment

Click here to post a comment

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

Get Exclusive Content

Join thousands of webmasters who get our free newsletter with tips on how to drive more traffic and revenue to their websites!

Please wait...

Thank you for sign up!

More in VPS
How to Change Hostname on CentOS 7
Close