Tail Command – How to Display a File’s Contents in 10 Ways

tail-command-in-linux

The tail command is one option for viewing the contents of a file. By default, the tail command will display the last 10 lines in the provided file. You can also utilize it to view real-time changes on a file, such as checking the log files.

You will often utilize the tail with the head command. While the tail command shows the last lines in the file, the head command does the opposite. It shows the first 10 lines in the target file. How do you use the tail Linux command? Read on to find out!

The Syntax and Options to Use with the tail Command

The command uses the below syntax.

Aside from understanding its syntax, the table below highlights some commonly used options with the command.

OptionDescription
-nIt sets the number of lines to include in the output.
-cIt displays the output based on specified bytes instead of lines.
-qIt removes the file names when displaying output.
-vIt displays information when running the tail command.
-sIt sets the wait time between iterations and is used alongside the -f option.

Examples of How to Use the tail Command

Let’s explore how you can use the tail command for different needs.

1. Basic Usage of the tail Command

The primary way of using the command is to display the last 10 lines in a file. First, let’s run the cat command to display the file’s contents. You will notice that running the cat command will include all the lines in the output.

linux-cat-command

However, only the last lines are displayed if we run the tail Linux command.

tail-command-in-linux

2. Show a Specific Number of Lines

Instead of displaying the last lines, you can specify how many lines to be displayed. The -n option will take the added number and run the tail command.

For instance, we are displaying only five lines with the below command.

tail-command-show-specific-lines

3. Set the Start Position

With the tail command, you can define where to start printing the lines using the +n option. The command will note the start position and print all the other lines from that position to the end of the file.

To set our start position at line 12, our command would be as follows.

tail-command-set-start-position

4. Use tail Command with Multiple Files

You can also display the contents of multiple files when running the command. Add the names of the target files and include any option.

We’ve displayed the last five lines for our two files.

tail-command-with-multiple-files

5. tail Command Suppress Filename

In the previous example, the output shows the file names of all the files you’ve used. However, you can remove the file names using the -q or (quiet mode).

tail-command-quiet-mode

6. Redirect Output

When you want to save the output of the tail command, you can use the redirect option and specify the name for the output file.

Once the command executes, run the cat command to display the contents of the output file. It will contain the exact output you would get when running the tail command.

redirect-output-of-tail-command

7. Display Output Based on Bytes

When you add the -c option, the tail command will display the last bytes instead of lines. The example demonstrates our results using the -c option with 30 bytes.

tail-command-work-with-bytes

8. Display Last Lines in Real-time

You can monitor changes on a file by using the -f option. The command will open the target file and display any new lines as they appear at the bottom.

For this example, we’ve opened a file and replaced its content using the cat command.

The tail command will show all the newly added lines to that file.

tail-command-in-real-time

9. Add Verbose

Adding the -v option will display the file name at the top of the output.

Here’s an example.

tail-command-show-file-name

10. Using the tail Command with Other Commands

Combining the command with other commands is possible when performing tasks. For instance, the ls command lists the contents of a directory. You can couple it with the tail command to only show the last elements in the displayed list.

We’ve limited our output to four lines.

tail-command-with-ls

When working with a long file, the grep command will help search for text in the file. Moreover, adding the tail Linux command will limit the search to only the last 10 lines in the file.

Here’s an example.

tail-command-with-grep

Conclusion

Knowing how to use the tail Linux command gives you an advantage when working with files. The tail command displays the last 10 lines in a file, and you can utilize its options to customize the output. We’ve discussed the tail command and given examples of using it.

Found this helpful? - Share it!

Leave a Comment

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

Scroll to Top