Using head and tail for printing the last or first 10 lines
When examining a large file, thousands of lines long, the cat command, which will display all the line,s is not suitable. Instead, we want to view a subset (for example, the first 10 lines of the file or the last 10 lines of the file). We may need to print the first n lines or last n lines or print all except the last n lines or all except the first n lines, or the lines between two locations.
The head and tail commands can do this.
How to do it...
The head command reads the beginning of the input file.
- Print the first 10 lines:
$ head file
- Read the data from
stdin:
$ cat text | head- Specify the number of first lines to be printed:
$ head -n 4 fileThis command prints the first four lines.
- Print all lines excluding the last
Mlines:
$ head -n -M file
Note
Note that it is negative M.
For example, to print all the lines except the last five lines, use the following command line:
$ seq 11 | head -n -5 1 2 3 4 ...