LinuxCommandLibrary

head

Display the beginning of a file

TLDR

Output the first few lines of a file

$ head -n [count] [path/to/file]
copy

SYNOPSIS

head [OPTION]... [FILE]...

PARAMETERS

-n NUM, --lines=NUM
    Output the first NUM lines instead of the default 10. If NUM is prefixed with a hyphen (e.g., -n -20), it outputs all but the last NUM lines (a GNU extension).

-c NUM, --bytes=NUM
    Output the first NUM bytes of each file. If NUM is prefixed with a hyphen (e.g., -c -500), it outputs all but the last NUM bytes (a GNU extension).

-q, --quiet, --silent
    Never print headers giving file names. This is useful when processing multiple files and only their combined content is desired, suppressing the default headers.

-v, --verbose
    Always print headers giving file names. This is useful when processing a single file and a header is desired for clarity, even if it's not strictly necessary.

-z, --zero-terminated
    Line delimiter is NUL, not newline. This option is primarily used when piping output to commands like xargs -0 to handle file names containing unusual characters or newlines.

--help
    Display a help message and exit.

--version
    Output version information and exit.

DESCRIPTION

The head command is a fundamental Linux and Unix utility used to display the beginning of text files or piped input to standard output. By default, it outputs the first 10 lines of each specified file. When multiple files are provided, head conveniently prefixes each output block with a header indicating the file name.

Users can easily customize the number of lines or bytes to be displayed, making head incredibly useful for quickly previewing the content of large files, examining log files, or checking configuration files without needing to open the entire file. It is often employed in conjunction with other commands via pipes, serving as an initial filter to narrow down data for further processing. head is the conceptual inverse of the tail command, which displays the end of files.

CAVEATS

When specifying a negative count (e.g., -n -NUM or -c -NUM), this functionality is a GNU Coreutils extension and may not be available on all Unix-like systems, which typically only support positive counts for head. Scripts relying on such features might lack portability. Additionally, outputting binary files directly to a terminal can result in garbled output or modify terminal settings, so caution is advised.

STANDARD VS. GNU EXTENSIONS

It's important to note that while the basic functionality of head (displaying the first N lines or bytes) is POSIX standard, certain options like specifying negative line/byte counts (e.g., -n -20) and the -z option are GNU Coreutils extensions. This means scripts relying on these specific features might not be portable to non-GNU Unix systems.

PIPING AND FILTERING

head is frequently used in shell pipelines to quickly inspect the output of other commands. For example, long_command | head -n 5 will show only the first five lines of long_command's output, making it an excellent tool for initial data exploration and debugging or to limit the amount of data processed by subsequent commands.

HISTORY

The head command is a venerable utility, having been a standard component of Unix-like operating systems since their early versions. Its fundamental purpose—displaying the beginning of files—has remained consistent throughout its evolution. As part of the GNU Coreutils package, the GNU version of head has introduced several enhancements, such as the ability to specify negative line/byte counts (to exclude the end of a file) and the --zero-terminated option, expanding its utility beyond the POSIX standard specification.

SEE ALSO

tail(1), cat(1), more(1), less(1), grep(1)

Copied to clipboard