Linux Quick Guide: Read the last part of files using tail
The tail
command allows you to take a look at the last few lines of a file:
tail [FILE]
Here is an example:
We can see the last 10 lines of the file linhas.txt. This is the default behaviour of the tail
command.
If it is desired to read a number of lines other than 10, the -n
option can be used to specify the number of lines to output:
Using -n 5
option, tail
displays only the last 5 lines from the file.
Learn more.
Most of modern Linux distributions allows you to omit the n
when using the -n
option.
For example:
Notice that tail -5
has the same effect of tail -n 5
.
How to read a file starting with a specified line number.
If we place a +
just before the number when using -n
option, tail
will begin printing from the specified line number:
The previous command tail -n +5
returned the content of file starting with the 5th line.
I use tail
this way when I want to remove the header line of a CSV file. When the first line of a CSV file is the header line, I can remove it using tail -n +2 file.csv
, which will output the content of file starting with the 2nd line.