In Unix, how can I display the first few lines of a file?

Use the Unix head command to read the first few lines of an input file and send them to standard output (i.e., 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:

  a
  b
  c

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 At IU, how do I get support for Linux or Unix?

This is document acri in the Knowledge Base.
Last modified on 2018-10-10 18:27:11.

Contact us

For help or to comment, email the UITS Support Center.