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:
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 alphabetfileThis command would return:
a b cYou 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:
To save this information into a file in the current directory named
mylist, you could enter:
For more information, consult the Unix manual page by entering at the Unix prompt:
man headAlso see:
Last modified on April 09, 2008.






