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.
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.
Table of Contents
- Using the Tail Command
- Tail Default Behavior
- Common Tail Options
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:
Common Tail Options
Print Specific Number of Lines
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
Print Starting at Specific Line
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
Print Last Number of Bytes
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
tail -c20 mTable5.txt tail --bytes=20 mTable5.txt
Print Bytes Starting at Specific Byte Position
You can also print bytes starting at a byte at a certain position (number)
tail -c+20 mTable5.txt tail --bytes=+20 mTable5.txt
Print Continuously (Follow Changes)
You can keep running tail even if the file has changed with
tail -f mTable5.txt tail --follow mTable5.txt
Print Continuously (Follow) Until Process is Terminated
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
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
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.