Linux Quick Guide: Read the first part of files using head
The head
command allows you to take a look at the first few lines of a file:
head [FILE]
Here is an example:
We can see the first 10 lines of the file linhas.txt. This is the default behaviour of the head
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, head
displays only the first 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 head -5
has the same effect of head -n 5
.
How to read a file skipping the last few lines.
If we place a -
just before the number when using -n
option, head
will print all but the last N lines of the file:
The previous command head -n -5
returned 15 of the 20 lines of linhas.txt. In other words, head
displayed all but the last 5 lines of the file.