head & tail

Description

The "head" & "tail" commands in UNIX can be used to view portions of a file. By default head displays first 10 lines of a file, and tail displays last 10 lines of a file.

Usage

The following UNIX commands are executed on the text file "num.txt" containing #s 1-100 in each line.

/* Display last 10 (default) lines of a file */
$ tail num.txt
91
92
93
94
95
96
97
98
99
100

/* Display first 10 (default) lines of a file */
$ head num.txt
1
2
3
4
5
6
7
8
9
10

Options

The mostly used options are:
-n    display n lines (for both head & tail) 
+n    display starting from line# n (only for tail)
/* Display last 5 lines of a file */
$ tail -5 num.txt
96
97
98
99
100

/* Display first 5 lines of a file */
$ head -5 num.txt
1
2
3
4
5

/* Display from line# 91 of the file (to the end) */
$ tail +91 num.txt
91
92
93
94
95
96
97
98
99
100

/* Display 5 lines from line# 91 */
$ tail +91 num.txt | head -5
91
92
93
94
95