How to use the tail command on Linux

The Linux tail Command displays data from the end of a file. It can even show updates added to a file in real time. We’ll show you how to use it.

Did systemd kill the tail?

the tail Command shows you data from the end of a file. Typically, new data is added at the end of a file, so the tail Command is a quick and easy way to see the latest additions to a file. It can also monitor a file and display any new text entry in that file as it occurs. This makes it a great tool for monitoring log files.

Many modern Linux distributions have the systemd System and service manager. This is the first process he has run Process ID 1, and it is the parent of all other processes. That role was earlier edited by the elders init System.

Along with this change came a new format for system log files. No longer in plain text, under systemd they are recorded in a binary format. to you need to use these log files to read the journactl Utility. the tail Command works with plain text formats. It does not read binary files. So does that mean the tail Is command a solution looking for a problem? Does it have anything else to offer?

There’s more to that tail Command as viewing updates in real time. Also, there are still many log files that are not system generated and that are still created as plain text files. To the example, log files generated by applications have not changed their format.

Use tail

Pass in the name of a file tail and it will show you the last ten lines from that file. the example Files we use contain lists of sorted words. Each line is numbered so it should be easy to follow the examples and see what effect the different options have.

tail word-list.txt

To display a different number of lines, use the -n (Number of lines) Option:

tail -n 15 word-list.txt

You can actually do without the “-n” and simply use a hyphen “-” and the number. Make sure there are no spaces in between. Technically, This is an obsolete form of commandbut it is still in the Manual page, and it still works.

tail -12 word-list.txt

Use tail with multiple files

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

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

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

Display lines at the beginning of a file

the + (count from the beginning) modifier makes tail Display lines from the beginning of a file, starting with a specific line number. If your file is very long and you choose a line close you will get a lot of output to the terminal window up to the beginning of the file. If so, it makes sense to route the output from tail into it less.

tail +440 list-1.txt

You can Scroll through the text in a controlled manner.

Since this file happens to contain 20,445 lines, this command is equivalent to using the -6 option:

tail +20440 list-1.txt

Using tailed bytes

You can say tail to use offsets in bytes instead of lines by using the -c (Byte) option. This can be useful when you have a text file that has been formatted in normal size records. Note that a newline character counts as one byte. This command displays the last 93 bytes in the file:

tail -c 93 list-2.txt

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

tail -c +351053 list-e.txt

Piping in the tail

Before that, we had the output of tail into it less . We can also route 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 direct the output to tail.

ls -tl | tail -5

the head command lists lines of text from the beginning of a file. We can combine that with tail to extract a section of the file. Here we use the head Command to extract the first 200 lines from a file. That is fed in tailthat extracted the last ten lines. This gives us lines 191 to line 200. These are the last ten lines of the first 200 lines:

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

This command lists the five most memory-hungry processes.

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

Let’s break that down.

the ps command shows information about running processes. The options used are:

  • a: Lists all processes, not just for the current user.
  • you: Displays a user-oriented output.
  • x: List all processes, including those that are not running in a TTY.

the sort command sorts the output from ps . The options we’re using with sort are:

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

the tail -5 Command displays the last five processes from the sorted output. These are the five most memory hungry processes.

Use tail to track files in real time

Keeping track of new text entries arriving in a file – usually a log file – is easy with tail. Pass the filename on the command line and use the -f (follow) 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 so that it only contains lines of particular relevance or interest. Here we use grep to show only lines that contain the word “average”:

tail -f geek-1.log | grep average

To track changes to two or more files, pass the file names on the command line:

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

Each entry has a header that shows which file the text comes from.

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

The display is updated every time a new entry arrives in a tracked file. To specify the update period, use the -s (Sleep period) option. That says tail wait a few seconds, five in this one example, between file checks.

tail -f -s 5 geek-1.log

Admittedly, you can’t tell from a screenshot, but the file is updated every two seconds. The new file entries are displayed in the terminal window every five seconds.

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

If you are following the text additions in more than one file, you can suppress the headers that indicate which log file the text came from. Use the -q (quietly) way to do this:

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

The output of the files is displayed in a seamless blend of text. There is no indication of which log file each entry came from.

Tail still has value

The system log files are now accessed by journalctl, tail still has a lot to offer. This is especially true when used in conjunction with other commands by typing in or out tail.

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

Linux commands
Filestar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · Rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processesalias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networkingnetstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts