tail command in linux. View the last lines of a file.

  • Last updated: Nov 3, 2023
  • Views: 547
  • Author: Admin
tail command in linux. View the last lines of a file.

Colleagues hello to all.

In today's article, we'll talk about such a command in Linux called tail. We all know about such a command as cat which is designed to view the entire contents of a file. But very often there are cases when we need to see only a part of the contents of a file or watch the contents of a file in real time.

 

Article content:

  1. Syntax for tail.
  2. List of options for the tail command.
  3. Usage examples.
  4. Summary

 

1. Syntax of the tail command.

Before we look examples of the Linux tail command, let's break down its syntax, it's very simple.

tail [options] file

By default, the utility will show you the last line of the file.


 

2. List of tail command parameters.

  • -c - prints the specified number of bytes.
  • -f - updates the information in the file as new lines appear.
  • -n - will show the last line count you specify instead of the last 10.
  • -q - never output headers with filenames.
  • -v - displays detailed information about the file.
  • -z - line separator.

 

3. Use examples.

Now that we already know what the command is responsible for and what parameters it has, let's look at usage examples.

 

3.1. The easiest example to see what we have at the end of the file is to execute:

$. tail demo-small-20170815.sql

linux_command_tail

The command displays the last ten lines, empty lines are also taken into account.

 

3.2. Well, if ten lines are not enough for us, then we can use the command with the -n parameter and specify the number that we need:

$. tail  -n 50 demo-small-20170815.sql

linux_command_tail

In this case, I indicated that I want to see the last 50 lines.

 

3.3. The most interesting thing is when you want to constantly serve the appearance of new lines in the file in real time. To do this, use the command with the -f parameter:

$. tail -f demo-small-20170815.sql

linux_command_tail

In this example, new lines are constantly monitored, and if they appear, you will immediately know about it.

 

3.4. We can also set how often to keep track of updates in the file. By default, the tail command with the -f option serves changes to the file every second, and with the -s option we can change the time to watch the file:

$. tail -f -s 10 demo-small-20170815.sql

linux_command_tail

 

3.5. Sometimes there are cases when I do not need all the contents of the file to be served, but only let's say when there is some kind of error. To do this, you can use the additional grep command:

$. tail -f demo-small-20170815.sql | grep error

linux_command_tail

Well, in the screenshot, we will not steal anything, since I have no errors in the file.


 

4. Results.

And so today we met our colleagues from a cool team for serving changes in files, and I strongly advise you to use it, as it saves you a lot of effort and nerves.


 

Thank you all, I hope that my article helped you in some way.

SIMILAR ARTICLES

Linux cp command, copying files

Linux cp command, copying files

Linux date command: how to set, change, format and display date

Linux date command: how to set, change, format and display date

cd command in linux. Description and examples.

cd command in linux. Description and examples.