head

file managementLinux/Unix
The head command is one of the most frequently used commands in Linux/Unix-like operating systems. head Output the first part of files

Quick Reference

Command Name:

head

Category:

file management

Platform:

Linux/Unix

Basic Usage:

head [options] [arguments]

Common Use Cases

    Syntax

    head [options] [file...]

    Options

    Option Description
    -c, --bytes=[-]NUM Print the first NUM bytes of each file; with leading '-', print all but the last NUM bytes
    -n, --lines=[-]NUM Print the first NUM lines instead of the first 10; with leading '-', print all but the last NUM lines
    -q, --quiet, --silent Never print headers giving file names
    -v, --verbose Always print headers giving file names
    -z, --zero-terminated Line delimiter is NUL, not newline
    --help Display help information and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the head command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    head file.txt
    Display the first 10 lines of file.txt.
    head -n 5 file.txt
    Display the first 5 lines of file.txt.
    head -c 20 file.txt
    Display the first 20 bytes of file.txt. # Advanced Examples Advanced head -n -15 file.txt Display all but the last 15 lines of file.txt. head -v -n 3 file1.txt file2.txt Display the first 3 lines of each file with headers. find . -name "*.log" -exec head -n 2 {} \; Display the first 2 lines of all log files in the current directory. head -q -n 1 *.conf Display the first line of all .conf files without headers.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The head command displays the beginning of a file or multiple files. It's commonly used to get a quick peek at the content of files, especially large ones, without having to open them entirely. Key features of head: 1. Line-Based Output: By default, head displays the first 10 lines of each specified file, which is useful for quickly examining file headers or the start of data files. 2. Byte-Based Output: With the -c option, head can output a specific number of bytes instead of lines, which is useful for binary files or when precise control over output size is needed. 3. Multiple File Support: head can process multiple files at once, displaying the beginning of each with a header indicating the filename. 4. Header Control: The -q (quiet) and -v (verbose) options allow controlling whether filenames are displayed as headers when processing multiple files. 5. Inverse Selection: By using a negative number with -n or -c, head can display all content except for the last N lines or bytes, effectively functioning as a complement to the tail command. 6. Zero-Terminated Lines: The -z option makes head treat null characters (\0) as line delimiters instead of newlines, which is useful when processing output from commands like find -print0. 7. Standard Input Support: If no file is specified or when - is used as a filename, head reads from standard input, making it versatile for use in command pipelines. head is particularly useful for tasks like checking file formats, viewing log file beginnings, examining data file headers, or quickly inspecting configuration files. It's often paired with other commands like grep, sort, or tail to perform more complex text processing operations.

    Related Commands

    These commands are frequently used alongside head or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the head command works in different scenarios.

    $ head
    View All Commands