5 practical Linux Tail command examples

Short description. Here are some practical examples of tail commands that should allow you to use the tail command more efficiently on Linux.

The Cat command is a small command to display the contents of a file. But you may not always be able to display all lines of a large file. The tail command helps with such a scenario.

What is the tail command?

The tail command, as the name suggests, prints the last parts of a single file or multiple files. By default, the tail command prints the last ten lines of the input files. The tail command is also used to read log files in real time.

The tail command syntax is:

tail [options] [files]

5 practical Linux Tail command examples

Let’s learn how to use tail command in Linux with some practical examples. We will be using this text file in this article:

Загадочное происшествие на Брайтон Бич
Тайный Противник
Убийство по ссылкам
Человек в коричневом костюме
Секрет дымовых труб
Погода в Кацапетовке
Большая четверка
Странный пассажир полуночного экспресса
Гарри Поттер
Все на Матч
Морские дьяволы
Статьи о Linux
Искусственный интеллект
SQL для начинающих
Дистрибутив Linux
Убийство в восточном экспрессе
Незаконченный Портрет
Почему они не спросили Эванса?
Три Акта Трагедии
Смерть в облаках

If you use the tail command without any option, it will print the last 10 lines. This is the default behavior of the tail command.

tail agatha.txt
Морские дьяволы
Статьи о Linux
Искусственный интеллект
SQL для начинающих
Дистрибутив Linux
Убийство в восточном экспрессе
Незаконченный Портрет
Почему они не спросили Эванса?
Три Акта Трагедии
Смерть в облаках

If the file has less than ten lines, it only displays the available lines. If the last few lines are empty, they will still be considered valid.

But you don’t have to settle for using the default tail command. That’s not all. Let’s take a look at the most common use of tail commands one at a time.

1. Print the last N lines with the tail command

To view the last N lines, instead of the default 10, you can use the tail command like this:

tail -n N <filename>

For example, if you want to see the last 5 lines of the tail command in our example file, you can use it like this:

tail -n 5 agatha.txt
Убийство в восточном экспрессе
Незаконченный Портрет
Почему они не спросили Эванса?
Три Акта Трагедии
Смерть в облаках

Tip You can also just use tail -N instead of tail -n N to display the last N lines of the file.

2. Print all lines starting from line number N

If you want to view all lines starting with line number N, you can use the + option here.

tail -n +N <filename>

In our example file, if you want to see all lines starting from line 7, you can use it like this:

tail -n +7 agatha.txt
Большая четверка
Странный пассажир полуночного экспресса
Гарри Поттер
Все на Матч
Морские дьяволы
Статьи о Linux
Искусственный интеллект
SQL для начинающих
Дистрибутив Linux
Убийство в восточном экспрессе
Незаконченный Портрет
Почему они не спросили Эванса?
Три Акта Трагедии
Смерть в облаках

3. Using multiple files with the tail command

The tail command allows you to use multiple files at the same time. All of the standard tail command options apply to multiple files.

tail -n N <file1> <file2> <file3>

The result is concatenated for all files, and by default the filename is displayed on lines starting with “==>”.

For example, if you wanted to see the last three lines of the sherlock.txt and agatha.txt files, it would look like this:

tail -n3 sherlock.txt agatha.txt
==> sherlock.txt <==
Приключение благородного холостяка
Приключение Берилл Коронет
Приключение Алисы в стране чудес

==> agatha.txt <==
Почему они не спросили Эванса?
Три Акта Трагедии
Смерть в облаках

Tip You can remove filenames from the output using quiet mode with the -q option.

4. Monitor files in real time with the tail command [Очень полезно для мониторинга журналов]

Let’s say you have a file and new content has been added to it. The tail command allows you to display all new lines as they are added to the file.

You can use the -f option for this.

tail -f <log-file>

The command will display the last 10 lines of the files first and then update the output when new lines are added to the file.

It is widely used to view log files in real time. This is perhaps the most practical use of the tail command.

Tip If you use -F instead of the -f option, the tail command will wait for the input file to be created (if it does not already exist) and then display the contents of the file in real time.

5. Using the tail command with pipes

The tail command can be used in conjunction with other commands using pipes.

For example, if you have too many files in a directory and you only want to see the last 3 changed files, you can use it like this:

ls -ltr | tail -n3

In the above command, ls -lrt lists all files in reverse chronological order. And then the tail command further parses that output and only displays the last three lines of the ls command output, and so we get a list of the three most recently modified files.

Bonus tip: show the tail command output with line numbers

Line numbers help in understanding and parsing the output. Suppose you have displayed the last 20 lines of a file, but you also want to see their line numbers so that you can see the common lines in the file.

Unfortunately there is no built-in option to display the output of the tail command with numbers.

But that doesn’t mean you can’t display line numbers with the tail command. For this you can use the power of the pipes. You just saw in the previous section that tail commands can be used with pipes. Why not use it here.

nl is a way to display the contents of a file with line numbers. If you combine it with the tail command using pipe, you get a display of the tail command with line numbers.

nl <filename> | tail -3

Hope you find the tail command examples useful. If you have any suggestions or questions, please share them in the comments section below.

Related Posts