The tail command

Yaser Rahmati | یاسر رحمتی

The tail command prints the last ten lines of a file.

Example:

[root@academy F1]# cat myfile.txt
Line 1 : content 1
Line 2 : content 2
Line 3 : content 3
Line 4 : content 4
Line 5 : content 5
Line 6 : content 6
Line 7 : content 7
Line 8 : content 6
line 9 : content 9
Line 10: content 10
Line 11: content 11
Line 12: content 12
[root@academy F1]# tail myfile.txt
Line 3 : content 3
Line 4 : content 4
Line 5 : content 5
Line 6 : content 6
Line 7 : content 7
Line 8 : content 6
line 9 : content 9
Line 10: content 10
Line 11: content 11
Line 12: content 12
[root@academy F1]#

Syntax:

tail [OPTION] [FILENAME]

Get a specific number of lines with tail:

Use the -n option with a number(should be an integer) of lines to display.

[root@academy F1]# tail -n 5 myfile.txt
Line 8 : content 6
line 9 : content 9
Line 10: content 10
Line 11: content 11
Line 12: content 12
[root@academy F1]#

This command will display the last five lines of the file myfile.txt.

Refresh the output on any new entry in a file

It is possible to let tail output any new line added to the file you are looking into. So, if a new line is written to the file, it will immediately be shown in your output. This can be done using the --follow or -f option. This is especially useful for monitoring log files.

Syntax:

tail -n <number> foo.txt

Additional Flags and their Functionalities

Short FlagLong FlagDescription

-c

--bytes=[+]NUM

Output the last NUM bytes; or use -c +NUM to output starting with byte NUM of each file

-f

--follow[={name|descriptor}]

Output appended data as the file grows; an absent option argument means 'descriptor'

-F

Same as --follow=name -- retry

-n

--lines=[+]NUM

Output the last NUM lines, instead of the last 10; or use -n +NUM to output starting with line NUM

Last updated