Linux Tail Command (With Examples)

Linux Tail Command (With Examples)

The tail command is native to Unix-like operating systems, BSD and FreeDOS. It is even now ported to Windows as a part of the unxutils package.

In Linux, it is shipped as a part of the package GNU coreutils.

The tail command outputs the “tail” (end) of a file or piped data.

By default, it prints out the last ten lines of what gets passed to it. You can use other options instead of the default behavior.

This article will explain how to use tail command, both the default behavior of tail and the options.

Why tail in the first place? Many programs write their recent status at the end of what’s called a log file, so it is great to have a shortcut to view the end of the file instead of scrolling down a huge file.

Using the Tail Command

Before explaining the usage of tail let’s define a file to operate on, for example let’s say we have a multiplication table for number 5 as follows.

We’ll need another file for an upcoming command usage so if we need another table, it will be the same format, these files will be named in this format mTable5.txt; also we’ll execute all of the commands in the same directory:

01x5=05
02x5=10
03x5=15
04x5=20
05x5=25
06x5=30
07x5=35
08x5=40
09x5=45
10x5=50
11x5=55
12x5=60

Tail Default Behavior

If we executed this command over this file with no option, it will print out the last 10 lines of the text file:

tail mTable5.txt

word image 94

Common Tail Options

Note: All of these commands operate at the end of your file, just like the default. If you want to print the beginning, you can use the complementary command head.

To print a certain number of lines instead of the default (10 lines), you can use either of these commands:

tail -n5 mTable5.txt
tail -5 mTable5.txt
tail --lines=5 mTable5.txt

word image 95

To print lines starting at a line with a certain number, these two commands have the same result:

tail -n+5 mTable5.txt
tail --lines=+5 mTable5.txt

word image 96

Instead of counting lines, you can print the last number of bytes (each character equals 4 bytes, including non-printable characters such as the end of line \n)

tail -c20 mTable5.txt
tail --bytes=20 mTable5.txt

word image 97

You can also print bytes starting at a byte at a certain position (number)

tail -c+20 mTable5.txt
tail --bytes=+20 mTable5.txt

word image 98

You can keep running tail even if the file has changed with -f or --follow options

tail -f mTable5.txt
tail --follow mTable5.txt

word image 99

You can add another condition to run tail, following a certain process and exit once the process is terminated or done

tail -f --pid=3237 mTable5.txt

word image 100

Once ps=3237 is done, tail terminates, and you get back to the command-line.

This number is the process id can be obtained for your process by executing ps -a and then searching for your process.

Tail Over Multiple Files at The Same Time

If you want to execute tail over many files at the same time, you need to be able to discriminate which lines belong to which file, you can use one of these commands.

tail -v mTable5.txt mTable6.txt
tail ---verbose mTable5.txt mTable6.txt

word image 101

Conclusion

In this tutorial, we explained how the tail command works, some of the different options that can be used with it, and we used examples to explain the usage of these options.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
Rm Command in Linux
Read More

Rm Command in Linux

The rm command is a very important command used to remove files and directories in Linux. The rm…
gnome logo with theme icon
Read More

How to Install GNOME Themes

Knowing how to install GNOME themes empowers you to customize the appearance of your Linux graphical user interface.…