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.txtTo spot a different number of lines, usage nan -n (number of lines) option:
tail -n 15 word-list.txtActually, 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.txtUsing 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.txtA 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.txtYou tin page done nan matter successful a controlled fashion.
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.txtUsing 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.txtYou 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.txtPiping 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 -5The 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 -10This bid lists nan 5 astir memory-hungry processes.
ps aux | benignant -nk +4 | tail -5Let'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.logAs each caller log introduction is added to nan log file, tail updates its show successful nan 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 averageTo travel nan changes to 2 aliases much files, walk nan filenames connected nan bid line:
tail -f -n 5 geek-1.log geek-2.logEach introduction is tagged pinch a header that shows which record nan matter came from.
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.logAdmittedly, 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.
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.logThe output from nan files is displayed successful a seamless blend of text. There is nary denotation which log record each introduction came from.
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.