How to Use the tail Command on Linux

Trending 3 weeks ago

Key Takeaways

  • The tail bid successful Linux displays information from nan extremity of a file, making it useful for monitoring log files and showing caller additions.
  • The preamble of systemd successful immoderate Linux distributions changed nan format of strategy log files to a binary format, but galore application-generated log files still usage plain matter format.
  • In summation to displaying updates successful real-time, tail tin besides beryllium utilized to show a circumstantial number of lines, activity pinch aggregate files, show lines from nan commencement of a file, usage byte offsets, and beryllium mixed pinch different commands done piping.

The Linux tail bid displays information from nan extremity of a file. It tin moreover show updates that are added to a record successful real-time. We show you really to usage it.

Did systemd Kill tail?

The tail bid shows you information from nan extremity of a file. Usually, caller information is added to nan extremity of a file, truthful nan tail bid is simply a speedy and easy measurement to spot nan astir caller additions to a file. It tin besides show a record and show each caller matter introduction to that record arsenic they occur. This makes it a awesome instrumentality to show log files.

Many modern Linux distributions person adopted the systemd strategy and work manager. This is nan first process executed, it has process ID 1, and it is nan genitor of each different processes. This domiciled utilized to beryllium handled by nan older init system.

Along pinch this alteration came a caller format for strategy log files. No longer created successful plain text, nether systemd they are recorded successful a binary format. To read these log files, you must use nan journactl utility. The tail bid useful pinch plain matter formats. It does not publication binary files. So does this mean nan tail bid is simply a solution successful hunt of a problem? Does it still person thing to offer?

There's much to nan tail bid than showing updates successful real-time. And for that matter, location are still plentifulness of log files that are not strategy generated and are still created arsenic plain matter files. For example, log files generated by applications haven't changed their format.

Using tail connected Linux

Pass nan sanction of a record to tail and it will show you nan past 10 lines from that file. The illustration files we're utilizing incorporate lists of sorted words. Each statement is numbered, truthful it should beryllium easy to travel nan examples and spot what effect nan various options have.

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

To spot a different number of lines, usage nan -n (number of lines) option:

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

Actually, you tin dispense pinch nan "-n", and conscionable usage a hyphen "-" and nan number. Make judge location are nary spaces betwixt them. Technically, this is an obsolete bid form, but it is still successful nan man page, and it still works.

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

Using tail With Multiple Files

You tin person tail activity pinch aggregate files astatine once. Just walk nan filenames connected nan bid line:

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

A mini header is shown for each record truthful that you cognize which record nan lines beryllium to.

Displaying Lines from nan Start of a FIle

The + (count from nan start) modifier makes tail show lines from nan commencement of a file, opening astatine a circumstantial statement number. If your record is very agelong and you prime a statement adjacent to nan commencement of nan file, you're going to get a batch of output sent to nan terminal window. If that's nan case, it makes consciousness to tube nan output from tail into less.

tail +440 list-1.txt
tail +44 list-1.txt successful a terminal window

You tin page done nan matter successful a controlled fashion.

Output from tail displayed successful little successful a terminal window

Because location hap to beryllium 20,445 lines successful this file, this bid is nan balanced of utilizing nan "-6" option:

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

Using Bytes With tail

You tin show tail to usage offsets successful bytes alternatively of lines by utilizing nan -c (bytes) option. This could beryllium useful if you person a record of matter that was formatted into regular-sized records. Note that a newline characteristic counts arsenic 1 byte. This bid will show nan past 93 bytes successful nan file:

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

You tin harvester nan -c (bytes) action pinch nan + (count from nan commencement of nan file) modifier, and specify an offset successful bytes counted from nan commencement of nan file:

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

Piping Into tail

Earlier, we piped nan output from tail into little . We tin besides tube nan output from different commands into tail.

To place nan 5 files aliases folders pinch nan oldest modification times, usage nan -t (sort by modification time) action pinch ls , and tube nan output into tail.

ls -tl | tail -5
ls -lt | tail -5 successful a terminal window

The caput bid lists lines of matter from nan commencement of a file. We tin harvester this pinch tail to extract a conception of nan file. Here, we're utilizing nan caput bid to extract nan first 200 lines from a file. This is being piped into tail, which is extracting nan past 10 lines. This gives america lines 191 done to statement 200. That is, nan past 10 lines of nan first 200 lines:

head -n 200 list-1.txt | tail -10
head -n 200 list-1.txt | tail -10 successful a terminal window

This bid lists nan 5 astir memory-hungry processes.

ps aux | benignant -nk +4 | tail -5
ps aux | benignant -nk +4 | tail -5 successful a terminal window

Let's break that down.

The ps bid displays accusation astir moving processes. The options utilized are:

  • a: List each processes, not conscionable for nan existent user.
  • u: Display a user-oriented output.
  • x: List each processes, including those not moving wrong a TTY.

The benignant bid sorts nan output from ps . The options we're utilizing pinch benignant are:

  • n: Sort numerically.
  • k +4: Sort connected nan 4th column.

The tail -5 bid displays nan past 5 processes from nan sorted output. These are nan 5 astir memory-hungry processes.

Using tail to Track Files successful Real-Time

Tracking caller matter entries arriving successful a record — usually a log record — is easy pinch tail. Pass nan filename connected nan bid statement and usage nan -f (follow) option.

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

As each caller log introduction is added to nan log file, tail updates its show successful nan terminal window.

Output from tail -f geek-1.log successful a terminal window

You tin refine nan output to see only lines of peculiar relevance aliases interest. Here, we're utilizing grep to only show lines that include nan connection "average":

tail -f geek-1.log | grep average
tail -f geek-1.log | grep mean successful a terminal window

To travel nan changes to 2 aliases much files, walk nan filenames connected nan bid line:

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

Each introduction is tagged pinch a header that shows which record nan matter came from.

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

The show is updated each clip a caller introduction arrives successful a followed file. To specify nan update period, usage nan -s (sleep period) option. This tells tail to hold a number of seconds, 5 successful this example, betwixt record checks.

tail -f -s 5 geek-1.log
tail -f -s 5 geek-1.log successful a terminal window

Admittedly, you can't show by looking astatine a screenshot, but nan updates to nan record are happening erstwhile each 2 seconds. The caller record entries are being displayed successful nan terminal model erstwhile each 5 seconds.

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

When you are pursuing nan matter additions to much than 1 file, you tin suppress nan headers that bespeak which log record nan matter comes from. Use nan -q (quiet) action to do this:

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

The output from nan files is displayed successful a seamless blend of text. There is nary denotation which log record each introduction came from.

Output from tail -f -q geek-1.log geek-2.log successful a terminal window

tail Still Has Value

Although entree to nan strategy log files is now provided by journalctl, tail still has plentifulness to offer. This is particularly existent erstwhile it is utilized successful conjunction pinch different commands, by piping into aliases retired of tail.

systemd mightiness person changed nan landscape, but there's still a spot for accepted utilities that conform to nan Unix accuracy of doing 1 point and doing it well.

Source Tutorials
Tutorials