Tail Command in Linux

FREE Online Courses: Enroll Now, Thank us Later!

In this article, we will learn everything about the tail command in Linux. We will look at what the tail command is, its uses, its syntax, and the options used along with it. In the end, we will look at some useful, practical examples of the tail command and its options to understand how they work. So pay attention, take notes and read the entire article for the best benefits.

What is linux tail command?

Tail is a command-line-based utility in Linux-based operating systems that prints the last “n” number of lines or data of the given input. This input can either be a file or a data stream. The head command is the exact opposite of the tail command.

You can determine the number of lines you want the tail command to print, but if you don’t specify anything, the tail command will automatically print the last ten lines of the input. The best part of the tail command is that you can also specify more than one input file. If you do so, data from each file is preceded by its name.

To get more information, you can even pipe the tail command to other shell commands like grep, sort, wc, and more. This is helpful when considering only the input’s last “n” lines. We can also pair the tail command with a range of options, which we will look at how to use in later sections of this article.

Syntax of the tail command

The syntax of the tail command is very straightforward:

tail <options> <file>

Let us look at the options present in the syntax of the tail command:

1. <options>

It takes various options specifying how the tail command must function, format, and print the output. You can specify multiple options in this field.

2. <file>

This option takes in the file’s name we want to pass as the input to the tail command. If you specify no filename, the tail command will read the input from the standard input. As discussed earlier, you can also enter multiple filenames in this field.

Options used with the tail command

Unlike most of the commands in Linux, the tail command has only a few options associated with it. Let us take a brief look at each one of them:

1. -c

This option prints the last specified number of bytes of each file. If you specify the number with a negative symbol (-), the tail command will act as the head command and print the first specified number of bytes of each file. You can also write this command as “–bytes.”

2. –help

This option prints the help menu of the tail command, as shown below:

help menu of the tail command

3. -n

This option prints the last specified number of lines instead of the last 10, which is the default setting. If you specify the number with a negative symbol (-), the tail command will act as the head command and print each file’s first specified number of lines. You can also write this command as “–lines.”

4. -q

This option never prints the headers identifying the file names. You can also write this command as “–quiet.”

5. –version

This option prints information regarding the version of the tail command.

version of the tail command

6. -v

This option provides verbose to the output by printing the headers.

7. -f

This option causes the tail command to loop forever by checking the files for new data at the end of the file or files.

8. –max-unchanged

This option looks for new data to print if the size changes by reopening the file.

9. –retry

This option tries to open a file even if it’s inaccessible temporarily. It is beneficial when paired with the option “–follow=name”.

Now that we have covered the basics and the theoretical part of the tail command, let us understand how the tail command and its options work by looking at some practical examples of it in the terminal of Ubuntu 20.04.

Before we head on to look at the examples, we need a sample text file to perform the tail command on, hence let us consider the sample text file shown below:

sample text file to test the tail command on

Using the tail command with no options

If we use the tail command followed by the name of the file you want the tail command to read the input from, it prints the last ten lines of the file as shown below:

using the tail command with no options

Displaying a specific number of lines

If you want to display a certain number of lines instead of the default 10, pair the tail command with the option “-n” followed by the number of lines you want to see and the filename:

tail -n <number> <filename>

displaying a specific number of lines

Displaying a specific number of bytes

To display a specific number of bytes, run the tail command along with the option “-c” followed by the number of bytes you want to see and the filename:

tail -c <number> <filename>

displaying a specific number of bytes

Displaying the filename

If you want to display the file’s name above the output of the tail command for a better understanding, combine the tail command with the option “-v”. This option provides verbose to the output for a better understanding. You can use the syntax as shown:

tail -v <filename>

displaying the filename

Displaying multiple files

If you want to use the tail command on multiple files, specify the filenames one after the other making sure to separate each filename by a space as shown in the syntax below:

tail <filename1> <filename2> <filename3>

displaying multiple files

Redirecting the output of the tail command

If you want to store the output produced by the tail command in a separate file, use the redirection operator followed by the name of the file as shown below:

tail <inputfile> > <outputfile>

You can later check if the file has been created or updated successfully by using the cat command as shown below:

redirecting the output of the tail command

Using linux tail command with data streams

We already saw earlier that we could also specify data streams as the input of the tail command. However, to do so, we simply can’t specify them as we have been doing it with regular files. So instead, we pipe the tail command with these data streams. Here are some examples:

using the tail command with data streams

Piping the tail command with other shell commands

As we discussed earlier, we can pipe the tail command with various other commands like wc, grep, sort, and many more. Here are a couple of examples:

In the screenshot below, we have passed the tail command input as the sort command’s input. Hence the output is sorted alphabetically.

piping the tail command with other shell commands

In the screenshot shown below, we have passed the output of the tail command as the input of the wc (word count) command. Hence, the output is the count information, including the number of lines, words, and characters.

passing the output of the tail command as the input of the wc command

In the screenshot below, we have passed the tail command output as the grep command’s input. Hence the output prints the lines with the word “usr” in them.

passing the tail command output as the grep command_s input

Summary

As you have seen, the last “n” number of lines or data of the given input are printed by the tail command. So you now know what the tail command is, why we use it, the syntax of the command, and also the options used along with it. We have also seen a few practical examples of the command in the Ubuntu terminal.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *