Linux Tail Command Options With Examples

July 1, 2013 | By
| 1 Reply More

The linux tail command does something like its name. It displays the last few lines of a file. It is mostly used for viewing log file updates as these updates are appended to the log files. This article explains tail command with most common options.

Tail Command Options

The tail command accepts input from STDIN. By default, the tail command displays last ten lines. So at STDIN, if you enter more than 10 lines, only last 10 lines will be displayed. In the following example, I will input numbers in descending order from 12 and after inputting 1, I will ctrl+d (i.e. end of file marker)

$ tail
12
11
10
9
8
7
6
5
4
3
2
1
10
9
8
7
6
5
4
3
2
1

With a file name as argument, it displays last 10 lines for that file.

$ tail file1.txt
memtest86+_multiboot.bin
System.map-2.6.38-12-generic
System.map-2.6.38-13-generic
System.map-2.6.38-8-generic
vmcoreinfo-2.6.38-12-generic
vmcoreinfo-2.6.38-13-generic
vmcoreinfo-2.6.38-8-generic
vmlinuz-2.6.38-12-generic
vmlinuz-2.6.38-13-generic
vmlinuz-2.6.38-8-generic

If multiple files are given as argument, it precedes each with a header displaying the name of the file.

$ tail file1.txt file2.txt
==> file1.txt <== memtest86+_multiboot.bin System.map-2.6.38-12-generic System.map-2.6.38-13-generic System.map-2.6.38-8-generic vmcoreinfo-2.6.38-12-generic vmcoreinfo-2.6.38-13-generic vmcoreinfo-2.6.38-8-generic vmlinuz-2.6.38-12-generic vmlinuz-2.6.38-13-generic vmlinuz-2.6.38-8-generic ==> file2.txt <== smi.conf snmp sound speech-dispatcher ssh ssl sudoers sudoers.d sysctl.conf sysctl.d

If you do not want to print the headers, you can use quiet mode with -q or --quiet or --silent option.

$ tail -q file1.txt file2.txt
memtest86+_multiboot.bin
System.map-2.6.38-12-generic
System.map-2.6.38-13-generic
System.map-2.6.38-8-generic
vmcoreinfo-2.6.38-12-generic
vmcoreinfo-2.6.38-13-generic
vmcoreinfo-2.6.38-8-generic
vmlinuz-2.6.38-12-generic
vmlinuz-2.6.38-13-generic
vmlinuz-2.6.38-8-generic
smi.conf
snmp
sound
speech-dispatcher
ssh
ssl
sudoers
sudoers.d
sysctl.conf
sysctl.d

And if you always want to print headers, even when only one file name is provided as argument, you can use -v or --verbose option

$ tail -v file1.txt
==> file1.txt <== memtest86+_multiboot.bin System.map-2.6.38-12-generic System.map-2.6.38-13-generic System.map-2.6.38-8-generic vmcoreinfo-2.6.38-12-generic vmcoreinfo-2.6.38-13-generic vmcoreinfo-2.6.38-8-generic vmlinuz-2.6.38-12-generic vmlinuz-2.6.38-13-generic vmlinuz-2.6.38-8-generic

Print last K number of lines

When you want to change the default behavior and change the last number of lines from 10 to something else, -n option is used

$ tail -n3 file1.txt
vmlinuz-2.6.38-12-generic
vmlinuz-2.6.38-13-generic
vmlinuz-2.6.38-8-generic

You can use -3 directly to print last 3 lines

$ tail -3 file1.txt
vmlinuz-2.6.38-12-generic
vmlinuz-2.6.38-13-generic
vmlinuz-2.6.38-8-generic

Print last K bytes

For printing the last K bytes of the file, use -c or --bytes=K option

$ tail -c 30 file2.txt
udoers.d
sysctl.conf
sysctl.d

This prints last 30 bytes of the file. Alternatively, you can use -c +K to output bytes starting with the Kth of each file.

$ tail -c +300 file1.txt
nfig-2.6.38-8-generic
grub
initrd.img-2.6.38-12-generic
initrd.img-2.6.38-13-generic
initrd.img-2.6.38-8-generic
memtest86+.bin
memtest86+_multiboot.bin
System.map-2.6.38-12-generic
System.map-2.6.38-13-generic
System.map-2.6.38-8-generic
vmcoreinfo-2.6.38-12-generic
vmcoreinfo-2.6.38-13-generic
vmcoreinfo-2.6.38-8-generic
vmlinuz-2.6.38-12-generic
vmlinuz-2.6.38-13-generic
vmlinuz-2.6.38-8-generic

View a growing file

When viewing some log file, if you want to check some changes being made at the end of the file, i.e. if you want to output appended data as the file grows, you will use -f or --follow option. For the illustration of this option, let us display the last 3 lines with follow option

$ tail -n3 -f file2.txt
sudoers.d
sysctl.conf
sysctl.d

You will not get the prompt back and it will wait for the appended data. Now in the other terminal, let us append some line in the file

$ echo "hello" >> file2.txt

It will appear on the previous terminal as

$ tail -n3 -f file2.txt
sudoers.d
sysctl.conf
sysctl.d
hello

You can adjust the sleep interval between the iterations, i.e. it will check after the provided number of seconds for the append updates with -s or --sleep-interval=N (default is 1) seconds.

$ tail -n 3 -f -s 4 file2.txt
sysctl.conf
sysctl.d
hello

The follow option does not exit by itself. But you can tell the command to terminate after some process with Process ID PID dies with --pid=PID option.

$ tail -f file2.txt --pid=4309
snmp
sound
speech-dispatcher
ssh
ssl
sudoers
sudoers.d
sysctl.conf
sysctl.d
hello

The command will exit as soon as the process with PID 4309 dies.

Linux Tail Command Version

$ tail --version
tail (GNU coreutils) 8.13
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Rubin, David MacKenzie, Ian Lance Taylor,
and Jim Meyering.

Linux tail command

Filed Under : LINUX COMMANDS

Free Linux Ebook to Download

Comments (1)

Trackback URL | Comments RSS Feed

  1. Xavier Bracco says:

    A good list of different ways of using this command, very nice to read....

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.