How to use the tail command in Linux

Contents

A terminal window displaying a Bash prompt on an Ubuntu-style Linux laptop.

El Linux tail The command displays data from the end of a file. It can even show updates that are added to a file in real time. We show you how to use it.

¿Systemd Kill Tail?

the tail The command displays data from the end of a file. In general, new data is added to the end of a file, so that tail The command is a quick and easy way to view the most recent additions to a file. You can also monitor a file and display each new text entry in that file as they occur.. This makes it a great tool for monitoring log files..

Many modern Linux distributions have adopted the systemd Systems and Services Manager. This is the first procedure executed, have Process id 1and is the parent of all other processes. This role used to be managed by elders init system.

Along with this change, came a new format for system log files. No longer created in plain text, on systemd are recorded in binary format. For read these log files, must use the journactl utility. the tail The command works with plain text formats. Doesn't read binary files. Then, this means that tail Is the command a solution in search of an obstacle? Do you still have something to offer?

There is more to it tail command to show realtime updates. And in reality, there are still many log files that are not generated by the system and are still created as plain text files. As an example, log files generated by Applications they have not changed their format.

Using glue

Pass the name of a file to tail and it will show you the last ten lines of that file. The example files we are using contain ordered word lists. Each line is numbered, so it should be easy to follow the examples and see what effect the various options have.

tail word-list.txt

To view a different number of lines, use el -n (number of lines) option:

tail -n 15 word-list.txt

Actually, you can do without the "-n" and use a hyphen "-" and the number. Make sure there are no gaps between them. Technically, this is a deprecated command form, but it's still in the man pageand it still works.

tail -12 word-list.txt

Using tail with many files

You can have tail work with many files at the same time. Just pass the filenames in the command line:

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

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

Viewing lines from the beginning of a file

the + (count from the beginning) modifier does tail show lines from the beginning of a file, starting at a specific line number. If your file is very long and you choose a line near the beginning of the file, you will get a lot of results sent to the terminal window. If that's the case, it makes sense to channel the output of tail within less.

tail +440 list-1.txt

You can leaf through the text in a controlled way.

Because there are 20,445 lines in this file, este comando es el semejante a utilizar la opción “-6”:

tail +20440 list-1.txt

Using trailed bytes

You can say tail to use offsets in bytes instead of lines using the -c (bytes) option. This can be useful if you have a text file formatted in normal size records. Note that a newline character counts as one byte. This command will display the latest 93 file bytes:

tail -c 93 list-2.txt

You can combine the -c (bytes) option with the + (count from start of file) and specify an offset in bytes counted from the start of the file:

tail -c +351053 list-e.txt

Pipe in the tail

Previously, we channel the output of tail within less . We can also channel the output of other commands to tail.

To identify the five files or folders with the oldest modification times, Use the -t (sort by modification time) option with ls and pipe the output to tail.

ls -tl | tail -5

the head command lists lines of text from the beginning of a file. We can combine this with tail to extract a section of the file. Here, we are using the head command to extract the first 200 lines of a file. This is channeled to tail, which extracts the last ten lines. This gives us the lines 191 up to the line 200. In other words, the last ten lines of the first 200 lines:

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

This command lists the top five memory consuming processes.

ps to | sort -nk +4 | tail -5

Let's analyze that.

the ps command displays information about running processes. The alternatives used are:

  • a: List all processes, not just for the current user.
  • your: Displays a user-oriented output.
  • X: List all processes, including those that do not run within a TTY.

the sort command order the exit from ps . The alternatives we are using with sort is it so:

  • North: Sort numerically.
  • k +4: Order in the fourth column.

the tail -5 The command displays the last five processes from the ordered output. These are the five processes that consume the most memory.

Using tail to track files in real time

Track new text entries arriving in a file, generally a log file, it's easy with tail. Pass the file name in the command line and use the -f (continue) option.

tail -f geek-1.log

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

You can refine the output to include only lines of particular relevance or interest. Here, we are using grep for only show lines that include the word “promedio”:

tail -f geek-1.log | grep average

To track changes in two or more files, pass filenames in command line:

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

Each entry is tagged with a header that shows which file the text came from.

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

The screen is updated every time a new entry arrives in a file followed. To specify the update period, Use the -s (rest period). This says tail wait several seconds, five in this example, between file checks.

tail -f -s 5 geek-1.log

It is true that you cannot tell by looking at a screenshot, but the updates to file are happening once every two seconds. New file entries are being displayed in terminal window once every five seconds.

Output of tail -f -s 5 geek-1.log

When you track text additions to more than one file, you can suppress the headers that indicate which log file the text comes from. Use the -q option (silent) to do this:

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

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

the tail still has value

Even though access to the system log files now provides it journalctl, tail still has a lot to offer. This is especially true when used in conjunction with other commands, entering or leaving tail.

systemd could have changed the landscape, but there is still a place for traditional utilities that conform to the Unix philosophy of doing one thing and doing it right.

setTimeout(function(){
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq = n;n.push=n;n.loaded=!0;n.version=’2.0′;
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s) } (window, document,’script’,
‘https://connect.facebook.net/en_US/fbevents.js’);
fbq(‘init’, ‘335401813750447’);
fbq(‘track’, ‘PageView’);
},3000);

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.