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 2017-05-16 11:51:36.

  • Fill out this form to submit your issue to the UITS Support Center.
  • Please note that you must be affiliated with Indiana University to receive support.
  • All fields are required.

Please provide your IU email address. If you currently have a problem receiving email at your IU account, enter an alternate email address.

  • Fill out this form to submit your comment to the IU Knowledge Base.
  • If you are affiliated with Indiana University and need help with a computing problem, please use the I need help with a computing problem section above, or contact your campus Support Center.

Please provide your IU email address. If you currently have a problem receiving email at your IU account, enter an alternate email address.