Saltar al contenido

How to use the tail command in Linux

How to move your Linux home directory to another drive

Systemd Kill Tail?

pigtailthe command displays data from the end of a file. New data is usually added to the end of a file so pigtailThe command is a quick and easy way to view the latest additions to a file. You can also monitor a file and display each new text entry in that file as it appears. This makes it an excellent tool for monitoring log files.

Many modern Linux distributions have adopted it systemdsystems and services administrator. This is the first process that runs, has Process ID 1 and he is the father of all other processes. This role was managed by initiate system older.

With this change came a new format for system log files. They are no longer created in plain text, below systemd are recorded in binary format. In order to read these log files, you must use them journactlutility. pigtailthe command works with plain text formats. It does not read binaries. So that means that pigtailIs ordering a solution looking for a problem? Do you have anything else to offer?

There are many more pigtailcommand to display real-time updates. And, in fact, there are still a lot of log files that are not generated by the system and are still created as plain text files. For example, log files generated by Applications they did not change the format.

Using glue

Pass a file name to pigtailand it will show you the last ten lines of that file. The sample files we use contain sorted word lists. Each line is numbered, so it should be easy to follow the examples and see what effect the different options have.

cola lista de palabras.txt

To see a different number of lines, use -noption (number of lines):

tail -n 15 lista de palabras.txt

tail -n 15 word-list.txt in a terminal window

In fact, you can do without «-n» and simply use a dash «-» and the number. Make sure there are no gaps between them. Technical, this is a deprecated order form, but is still in page man and it still works.

tail-lista de 12 palabras.txt

tail -12 word-list.txt in a terminal window

Using multi-file code

Can pigtailwork with multiple files at the same time. Just type the file names on the command line:

tail -n 4 lista-1.txt lista-2.txt lista-3.txt

tail -n 4 list-1.txt list-2.txt list-3.txt in a terminal window

A small header is displayed for each file, so you know which file the lines belong to.

View lines at the beginning of a file

+the modifier (start number) does pigtail displays lines at the beginning of a file, starting at a certain line number. If your file is very long and you choose a line near the beginning of the file, you will receive a lot of results sent to the terminal window. If this is the case, it makes sense to move from the exit pigtail inside Less.

tail +440 list-1.txt

tail +44 list-1.txt in a terminal window

Can browse the text in a controlled way.

The output of the queue is displayed minus in a terminal window

Because there are 20,445 lines in this file, this command is the equivalent of using the «-6» option:

tail +20440 list-1.txt

tail +20440 list-1.txt in a terminal window

Using tracked bytes

I can tell you pigtailto use byte offsets instead of lines that use -coption (bytes). This can be useful if you have a text file formatted in normal size records. Note that a newline character counts as a byte. This command will display the last 93 bytes of the file:

tail -c 93 lista-2.txt

tail -c 93 list-2.txt in a terminal window

You can combine -coption (bytes) with +modifier (number at the beginning of the file) and specify an offset in bytes counted from the beginning of the file:

tail -c +351053 list-e.txt

tail -c +351053 list-e.txt in a terminal window

Pipe in the tail

Previously, I channeled the output from pigtailto Less. We can also channel the output of other commands to pigtail.

To identify the five files or folders with the oldest changes, use toption (sort by change time) with I amand the outlet pipe to pigtail.

ls -tl | cola -5

ls -lt |  queue -5 in a terminal window

headcommand lists the lines of text at the beginning of a file. We can combine this with pigtailto extract a section from the file. Here, we use headcommand to extract the first 200 lines of a file. This is channeled pigtail, which extracts the last ten lines. This gives us lines 191 to line 200. That is, the last ten lines of the first 200 lines:

head -n 200 list-1.txt | cola -10

head -n 200 list-1.txt |  queue -10 in a terminal window

This command lists the first five memory-consuming processes.

ps aux | sort -nk +4 | cola -5

ps aux |  sort -nk +4 |  queue -5 in a terminal window

Let’s look at this.

pscommand displays information about running processes. The options used are:

  • to : List all processes, not just for the current user.
  • or – Displays user-oriented output.
  • X – lists all processes, including those that do not run in a TTY.

kindcommand command the output from ps. The options we use kindAre:

  • n : Sorts numerically.
  • k +4 : Sort by fourth column.

tail -5the command displays the last five processes in the sorted output. These are the five processes that consume the most memory.

Use the code to track files in real time

It’s easy to keep track of new text entries coming in a file, usually a log file pigtail. Enter the file name on the command line and use -Foption (continued).

tail -f geek-1.log

tail -f geek-1.log in a terminal window

As each new log entry is added to the log file, tail updates its display in the terminal window.

Exit tail -f geek-1.log in a terminal window

You can refine the result to include only lines of special relevance or interest. Here, we use grepin order to show only the lines that include the word «environment»:

tail -f geek-1.log | promedio de grep

tail -f geek-1.log |  medium grep in a terminal window

To track changes in two or more files, enter the file names on the command line:

tail -f -n 5 geek-1.log geek-2.log

tail -f -n 5 geek-1.log geek-2.log in a terminal window

Each entry is labeled with a header that shows where the text came from.

Queue output -f -n 5 geek-1.log geek-2.log

The screen is updated each time a new entry arrives in a tracked file. To specify the update period, use -soption (suspension period). This tells you pigtail Wait a few seconds, five in this example, between file checks.

tail -f -s 5 geek-1.log

tail -f -s 5 geek-1.in a terminal window

It’s true that you can’t tell by looking at a screenshot, but at updates from the file it is performed every two seconds. The new file entries are display in the terminal window once every five seconds.

Queue output -f -s 5 geek-1.log

When tracking text additions to multiple files, you can delete titles that indicate which log file the text came from. uses q(silent) option to do this:

tail -f -q geek-1.log geek-2.log

tail -f -q geek-1.log geek-2.log in a terminal window

The output of the files is displayed in a perfect text combination. There is no indication in the log file from each entry.

-F -q code output geek-1.log geek-2.log in a terminal window

the tail still has value

Although access to system log files is now provided journalctl, pigtail it still has a lot to offer. This is especially true when used in conjunction with other commands, input or output pigtail.

systemd It could have changed the landscape, but there is still room for traditional utilities that conform to the Unix philosophy of doing something and doing it well.

.