- Unix Commands Reference
- Unix Commands - Home
head command in Linux with Examples
Name
head - display the beginning of a text file or piped data
Synopsis
head [OPTION]... [FILE]...
Options
-c, --bytes=[-]NUM print the first NUM bytes of each file; with the leading '-', print all but the last NUM bytes of each file -n, --lines=[-]NUM print the first NUM lines instead of the first 10; with the leading '-', print all but the last NUM lines of each file -q, --quiet, --silent never print headers giving file names -v, --verbose always print headers giving file names -z, --zero-terminated line delimiter is NUL, not newline --help display this help and exit --version output version information and exit
Description
head
command is a command-line utility, which prints the first 10 lines of the specified files. If more than one file name is provided then data from each file is preceded by its file name.
We can change the number of lines the head command prints by using the -n command line option. Not only number of lines, you can also restrict the head command output to a specific number of bytes. This can be done using the -c command line option.
For compatibility head
also supports an obsolete option syntax ‘-[NUM][bkm][clqv]’, which is recognized only if it is specified first, before the filename. NUM is a decimal number optionally followed by a size letter (‘b’, ‘k’, ‘m’) b for blocks, k for kilo and m for mega. NUM when used with -c means NUM bytes and NUM when used with -l means number of lines. Scripts intended for current Linux systems should use ‘-c NUM’ or ‘-n NUM’ instead.
An exit status of zero indicates success, and a nonzero value indicates failure.
Examples
Let us consider two files having name test1.txt and test2.txt
$ cat test1.txt a) 1122 b) 2233 c) 3344 d) 4455 e) 5566 f) 6677 g) 7788 h) 8899 i) 9910 j) 1011 k) 1112 l) 1213 m) 1314 n) 1415 o) 1516 p) 1617 $ cat test2.txt a) a.k. shukla b) anat hari c) barun kumar d) jai sharma e) sumit singh f) jamun singh g) naman kumar h) mosin khan i) rahul kumar j) ajay narayan k) ram naresh l) ashu choudhary m) pandit ramphal n) gagan choubey o) utar purn p) kovid puram
Passing only file name in the argument will display only the first 10 lines of the file.
$ head test1.txt a) 1122 b) 2233 c) 3344 d) 4455 e) 5566 f) 6677 g) 7788 h) 8899 i) 9910 j) 1011
we can also pass more than one file name in the argument, This will output the first 10 lines of each file to standard output with a header indicating which file is being shown.
$ head test1.txt test2.txt ==> test1.txt <== a) 1122 b) 2233 c) 3344 d) 4455 e) 5566 f) 6677 g) 7788 h) 8899 i) 9910 j) 1011 ==> test2.txt <== a) a.k. shukla b) anat hari c) barun kumar d) jai sharma e) sumit singh f) jamun singh g) naman kumar h) mosin khan i) rahul kumar j) ajay narayan
Here in the above example files are seperated by header i.e filename, to suppress the header line pass the -q option. This can be useful to combine files.
$ head -q test1.txt test2.txt a) 1122 b) 2233 c) 3344 d) 4455 e) 5566 f) 6677 g) 7788 h) 8899 i) 9910 j) 1011 a) a.k. shukla b) anat hari c) barun kumar d) jai sharma e) sumit singh f) jamun singh g) naman kumar h) mosin khan i) rahul kumar j) ajay narayan
While 10 is the default number of lines the head command prints, we can change this number as per the requirement. The -n command line option lets you do that.
$ head -n 5 test1.txt test2.txt ==> test1.txt <== a) 1122 b) 2233 c) 3344 d) 4455 e) 5566 ==> test2.txt <== a) a.k. shukla b) anat hari c) barun kumar d) jai sharma e) sumit singh
It should be noted that when we use the "head -n NUM [Filename]" command, then we must specify the integer i.e NUM, otherwise it will result to an error.
We can limit the number of bytes shown with head pass the -c option. Instead of limiting by number of lines this will limit by the number of bytes passed to the -c option.
In the following example the output is limited to 32 bytes.
$ head -c 32 test2.txt a) a.k. shukla b) anat hari c) b
You can also use a multiplier suffix after the number to specify the number of bytes to be shown. b multiplies it by 512, kB multiplies it by 1000, K multiplies it by 1024, MB multiplies it by 1000000, M multiplies it by 1048576, and so on.
The following command will display the first two kilobytes (2048) of the file filename.txt:
$ head -c 2kB test1.txt test2.txt ==> test1.txt <== a) 1122 b) 2233 c) 3344 d) 4455 e) 5566 f) 6677 g) 7788 h) 8899 i) 9910 j) 1011 k) 1112 l) 1213 m) 1314 n) 1415 o) 1516 p) 1617 ==> test2.txt <== a) a.k. shukla b) anat hari c) barun kumar d) jai sharma e) sumit singh f) jamun singh g) naman kumar h) mosin khan i) rahul kumar j) ajay narayan k) ram naresh l) ashu choudhary m) pandit ramphal n) gagan choubey o) utar purn p) kovid puram
We can can combine the head command with some other linux commands to perform certain operations.
We can print the lines of a file in a specific range.
$ head -n 15 test1.txt | tail -10 f) 6677 g) 7788 h) 8899 i) 9910 j) 1011 k) 1112 l) 1213 m) 1314 n) 1415 o) 1516
The above stated command will print from line 6 to line 15 of test1.txt.