head command (displays the first part of files)
- Organization of this page
Format
head [-num-lines|-n num-lines] [path-name ...]
Description
This command displays the first few lines from one or more files. The specified number of lines from the beginning of the file are output to the standard output. If no file is specified, the standard input is read. If no value is specified for the number of lines to be output, 10 lines is assumed.
Arguments
- -num-lines | -n num-lines ~<decimal>((1 to 2147483647))
-
Specifies the number of lines from the beginning of the input file that are to be sent to the standard output. If you specify a value that is less than or equal to 0 or that is greater than 2147483647, an error message (head: line count too small: specified-value or head: line count too large: specified-value) is output.
- path-name
-
Specifies the path name of an input file.
-
The default is the standard input.
-
Multiple files can be specified. If you specify more than one file, each file is identified at the beginning of the output from that file by a blank line (linefeed) and its file name in a header string in the following format:
==> file-name <==
-
When you execute the command with multiple files specified, all the files are processed. If any file fails to open, the command terminates with a return code of 1.
-
Return codes
Return code |
Meaning |
---|---|
0 |
Normal termination |
1 or greater |
Error termination |
Usage examples
The following shows the format of the files used in the examples below to illustrate the results of executing the head command.
-
test1.txt
0001:test1.txt 0002:test1.txt 0003:test1.txt 0004:test1.txt 0005:test1.txt 0006:test1.txt 0007:test1.txt 0008:test1.txt 0009:test1.txt 0010:test1.txt
-
test2.txt
0001:test2.txt 0002:test2.txt 0003:test2.txt 0004:test2.txt 0005:test2.txt 0006:test2.txt 0007:test2.txt 0008:test2.txt 0009:test2.txt 0010:test2.txt
The examples below illustrate the results of executing the command on the files shown above.
-
Display the first two lines of the files test1.txt and test2.txt.
$ head -2 test1.txt test2.txt ==> test1.txt <== 0001:test1.txt 0002:test1.txt ==> test2.txt <== 0001:test2.txt 0002:test2.txt $
-
Display an option error message.
This message might vary depending on the platform on which the command is executed. The following shows an example in Windows:
C:\TEMP>%ADSH_OSCMD_DIR%\head -d head: illegal option -- d usage: head [-count | -n count] [file ...]