Showing the last lines of a file with Linux tail command

Showing the last lines of a file with Linux tail command.

One of the most commonly used commands in Linux systems to handle the date inside the files is the tail command which basically, prints the last lines of it as you may presume with the command name.

Although it seems pretty simple, it is very used by the Linux community and thanks to its command options, it could be very useful when it comes to log processing.

Printing the ending of a file with tail command by line count

So let’s begin with an example of how can you use it and in which situations. Let’s say that we have a long file called “long_file” (up to 100 lines) created by the following script:

#!/bin/bash
for i in {1..100}; do
        echo "$i hello World" >> long_file
done

if you print the file with the cat command, it wont fit the size of the terminal and depending on which files (really big files), it could keep your terminal busy for a moment. On the other hand, you want a very fast and handy way to watch the last lines of a log file without open it with interactive commands like vi or less. So here’s where tail command comes into play to print the last lines of the files without running browsing style command and then type a command to jump to the last lines:

Linux tail command example. Showing last ten lines of a file.

If you run the tail command without any options, it will print the last 10 lines of the target file. But, what if you want to print less lines or print more lines? To do so, the tail command has to be executed with the -n option, followed by the number of lines you want to print to leverage in the behaviour of the command:

$ tail -n 15 long_file
Linux tail command example with -n option. Showing last 15 lines of a file.

The above picture, shows the tail command printing the last 15 lines. The -n option can be also –lines:

$ tail --lines 14 long_file
Linux tail command example with --lines option. Similar to -n. Showing last 14 lines of a file.

Printing the last characters or bytes of the file

Similarly, you can get the last bytes of the file with -c option or, in other words, the last number of characters of the file:

$ tail -c 26 long_file 
Linux tail command example with -c option to print the last 26 characters of the file.

As the -n option, the -c option has a verbose form which is the –-bytes:

$ tail --bytes 39 long_file
Linux tail command example with --bytes option to print the last 39 bytes or characters of the file. It works similar to -n

Show the ending of a file and the appended new lines with tail

This is one of the most helpful options that the tail command can deliver and for troubleshooting or auditing the system, it could serve as a real time log file viewer.

To do so, you need to run tail with the option -f or –follow and next the target file name path:

$ tail -f long_file
Linux tail -f or --follow example that shows how the last lines of the file are printed in real time in case it is updated after executing the command.

As you may see in the above picture, it shows two terminals that explains this use case.

On the left, the tail -f command that shows the last lines and will print the last appended files until the command is forced to terminate like by issuing a CTRL+C. Then on the right, another terminal that simulates another process appending new data at the end of the file, to trigger the tail print update on the left terminal.