The tail command in Linux displays the last few lines/blocks of any file, the number of lines/blocks being dependent on the options passed when using the command.
Also read: The uptime Command in Linux
The Linux tail command default output
When no options are provided, the tail command prints the last 10 lines for all the input files provided as arguments.
root@HowLinux:~# cat input.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
root@HowLinux:~# tail input.txt
11
12
13
14
15
16
17
18
19
20
In addition to this, we can use the +
sign to signify that the position is relative to the beginning of the input. For example, tail +15 input.txt
prints input.txt
starting from line number 15, until the end of the file.
root@HowLinux:~# tail +15 input.txt
15
16
17
18
19
20
Linux tail Command Options
OPTION NAME | DESCRIPTION |
-b ${NUM} | Displays the last NUM of 512-byte blocks (the last 512*NUM bytes are printed) |
-c ${NUM} | Displays the last NUM of bytes of the file (the last NUM bytes of the file are printed) |
-f | Keeps track of the file descriptor (Only takes a single file name as argument) |
-F | Same as f but also checks if the filename corresponding to the file descriptor is renamed |
-n ${NUM} | Displays the last NUM lines of the file |
-r | Displays the input in reverse order. It can be used in combination with other options |
1.The blockwise (-b) option
Format : tail -Nb input1.txt input2.txt ...
This is used to display the last N
blocks of 512 bytes of all the input files.
root@HowLinux:~# tail -1b input.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2.The character-wise (-c) option
Format: tail -c N input1.txt input2.txt ...
This is used to display the last N
characters(bytes) of all the input files
root@HowLinux:~# tail -c 10 input1.txt input2.txt
==> input1.txt <==
18
19
20
==> input2.txt <==
18
19
20
3.The line-number (-n) option
Format: tail -n N input1.txt input2.txt ..
OR
Format: tail -N input1.txt input2.txt ..
This option displays the last N
lines of all the input files
root@HowLinux:~# tail -n 10 input.txt
11
12
13
14
15
16
17
18
19
20
4.The verbose (-v) option
Format: tail -v input1.txt input2.txt ..
Displays the file header before the output of that file
root@HowLinux:~# tail -v input1.txt input2.txt
==> input1.txt <==
11
12
13
14
15
16
17
18
19
20
==> input2.txt <==
11
12
13
14
15
16
17
18
19
20
Multiple options can be combined with -v
to display with the corresponding file headers
The below command combines the -n
option with -v
, displaying the last 5 lines along with the file headers
root@HowLinux:~# tail -rn5 input.txt
==> input.txt <==
16
17
18
19
20
5. The flag options (-f, -F)
These are primarily used for log files, or when certain important files need to be tracked. This does not stop after reaching the end of the file, but rather keeps track of the file descriptor and updates the output whenever there is a change to the file descriptor. This command can only be stopped if the file descriptor is closed or when the Interrupt signal is received.
Also read: The head Command in Linux
Summary
We learned how the tail command can be used to display the contents of the file in different ways, depending on the option specified.
References
Linux Manual page: http://man7.org/linux/man-pages/man1/tail.1.html