Display the first few lines of a file in Unix

Use the Unix head command to read the first few lines of an input file and send them to standard output (that is, your terminal screen). The format for the head command is:

  head -lines filename

In this example, lines is an optional value specifying the number of lines to be read. If you don't give a number, the default value of 10 is used. Also, filename represents the name of an optional file for the head command to read. Otherwise, it will take its input from stdin (standard input: the terminal, or whatever the shell feeds the process with, usually pipe output).

For example, given a file containing the English alphabet with each letter on a separate line, a user enters the command:

head -3 alphabetfile

This command would return:


You can also use the head command with pipes. For example, to see the sizes of the first few files in a large directory, you could enter at the Unix prompt:

ls -l | head

To save this information into a file in the current directory named mylist, you could enter:

ls -l | head > mylist

For more information, consult the Unix manual page by entering at the Unix prompt:

man head

At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.

This is document acri in the Knowledge Base.
Last modified on 2023-07-18 16:13:31.