Skip to main content

Command Palette

Search for a command to run...

Cheatsheet & Examples: head

Updated
3 min read
H

I am a developer from Malaysia. I work with PHP most of the time, recently I fell in love with Go. When I am not working, I will be ballroom dancing :-)

The head command displays the beginning of a file, typically the first 10 lines by default. It is useful for quickly previewing files without opening them entirely.

Display the first 10 lines of a file

Example Usage:
head filename.txt

What it does:
Prints the first 10 lines of the specified file.

Command-line Arguments Explained:

  • filename.txt: The file whose content is to be displayed.

Display a specified number of lines

Example Usage:
head -n 5 filename.txt

What it does:
Prints the first 5 lines of the file.

Command-line Arguments Explained:

  • -n 5: Specifies the number of lines to display (in this case, 5).
  • filename.txt: The file to be read.

Display a specified number of bytes

Example Usage:
head -c 100 filename.txt

What it does:
Prints the first 100 bytes of the file.

Command-line Arguments Explained:

  • -c 100: Specifies the number of bytes to display (in this case, 100).
  • filename.txt: The file to be read.

Suppress headers when viewing multiple files

Example Usage:
head -q -n 3 file1.txt file2.txt

What it does:
Displays the first 3 lines of each file without printing filenames as headers.

Command-line Arguments Explained:

  • -q: Suppresses the display of filenames for multiple files.
  • -n 3: Specifies the number of lines to display (3 in this case).

Display the first 10 lines of standard input

Example Usage:
head

What it does:
Reads and displays the first 10 lines of input from the standard input stream (e.g., user input or piped data).

Command-line Arguments Explained:

  • No arguments: Uses default behavior (first 10 lines).

Display the first 10 lines of multiple files

Example Usage:
head file1.txt file2.txt

What it does:
Prints the first 10 lines of each specified file, including their filenames as headers.

Command-line Arguments Explained:

  • file1.txt: The first file to be read.
  • file2.txt: The second file to be read.

Display the first 5 lines of a file and write to another file

Example Usage:
head -n 5 filename.txt > output.txt

What it does:
Extracts the first 5 lines of filename.txt and saves them to output.txt.

Command-line Arguments Explained:

  • -n 5: Specifies the number of lines to display (5 in this case).
  • filename.txt: The source file.
  • > output.txt: Redirects the output to a new file.

Follow a file's content in real time

Example Usage:
head -f log.txt

What it does:
Continuously displays new lines added to log.txt as they are written, useful for monitoring logs.

Command-line Arguments Explained:

  • -f: Enables "follow" mode, keeping the command running to show updates.
  • log.txt: The file to monitor for new content.

Display the first 10 lines of a file with verbose output

Example Usage:
head -v filename.txt

What it does:
Prints the first 10 lines of the file, along with the filename as a header (even for a single file).

Command-line Arguments Explained:

  • -v: Enables verbose mode, showing filenames for individual files.
  • filename.txt: The file to be read.

More from this blog

Hong's Tech Blog

110 posts

The blog is older than you know. I prefer counting from the emergence of one integral anomaly to the emergence of the next, in which case this is the forth version.