head (Unix)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

Operating systemUnix, Unix-like, MSX-DOS

head is a program on Unix and Unix-like operating systems used to display the beginning of a text file or piped data.


The command syntax is:

head [options] <file_name>

By default, head will print the first 10 lines of its input to the standard output. The number of lines printed may be changed with a command line option. The following example shows the first 20 lines of filename:

head -n 20 filename

This displays the first 5 lines of all files starting with foo:

head -n 5 foo*

Most versions allow omitting the n and just let you say -5. GNU head allows negative arguments for -n option, meaning to print all but the last - argument value counted - lines of each input file.


-c <x number of bytes> Copy first x number of bytes.


Many early versions of Unix did not have this command, and so documentation and books had sed do this job:

sed 5q filename

This says to print every line (implicit), and quit after the fifth.

If you want to do the reverse of what head does. For example: not display the first 5 lines of a file, this would do the job

 sed -n '1,5!p'  "filename"


A head command is also part of ASCII's MSX-DOS2 Tools for MSX-DOS version 2.[1]

See also[edit]


External links[edit]