FactorPad
Build a Better Process

Linux head Command Summary with Examples

Linux head allows you to print the first lines of a file.
  1. Purpose - Learn what head is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with head.
  4. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 23, 2021
In this tutorial on Linux head, 27 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the videos.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / head-command.html


An ad-free and cookie-free website.


Examples of the Linux head Command

Beginner

Learn to print the top part of a file at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux head Command Summary with Examples (3:38)

Video Script

The Command and Why You Need It

Our twenty-seventh word, or command to memorize is head from our category Text.

head allows you to print the first lines of a file.

Common Linux head Options
-options description
--help Print help screen
-n, --lines Print the first number of lines specified
-c, --bytes Print the first number of bytes specified

Recall from the previous video we counted lines in a file. Now we'll scope out only those we're interested in reviewing.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The head command has 6 options and the argument is the file you'd like to review.

Like most commands, help is available with double-dash --help.

The default is to print the first 10 lines, and the -n option allows you to view more or less than 10. And just like in the last video (tutorial) on wc, it's -c and not dash-b, for bytes.

So why is head an important command? Well, head helps you get results quickly. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go, let's look at the first 10 lines of the file from the previous video, which was the help screen on the command wc.

$ head video26.txt Usage: wc [OPTION]... [FILE]... or: wc [OPTION]... --files0-from=F Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. With no FILE, or when FILE is -, read standard input. A word is a non-zero-length sequence of characters delimited by white space. The options below may be used to select which counts are printed, always in the following order: newline, word, character, byte, maximum line length. -c, --bytes print the byte counts -m, --chars print the character counts

See how it prints right to the screen? Pretty easy huh?

Second, let's create a new file here using head --help and redirect that to a file called video27.txt using the redirection > symbol.

$ head --help > video27.txt $ ls stuff video11.txt video19.txt video21.txt video24.txt video27.txt video05.txt video16.txt video20.txt video23.txt video26.txt

Then let's cat that to the screen.

$ cat video27.txt Usage: head [OPTION]... [FILE]... Print the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name. With no FILE, or when FILE is -, read standard input. Mandatory arguments to long options are mandatory for short options too. -c, --bytes=[-]K print the first K bytes of each file; with the leading '-', print all but the last K bytes of each file -n, --lines=[-]K print the first K lines instead of the first 10; with the leading '-', print all but the last K lines of each file -q, --quiet, --silent never print headers giving file names -v, --verbose always print headers giving file names --help display this help and exit --version output version information and exit K may have a multiplier suffix: b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024, GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y. GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Full documentation at: <http://www.gnu.org/software/coreutils/head> or available locally via: info '(coreutils) head invocation'

Too much to look at, huh? So let's head the first 17 (lines) using -n 17 the file video27.txt.

$ head -n 17 video27.txt Usage: head [OPTION]... [FILE]... Print the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name. With no FILE, or when FILE is -, read standard input. Mandatory arguments to long options are mandatory for short options too. -c, --bytes=[-]K print the first K bytes of each file; with the leading '-', print all but the last K bytes of each file -n, --lines=[-]K print the first K lines instead of the first 10; with the leading '-', print all but the last K lines of each file -q, --quiet, --silent never print headers giving file names -v, --verbose always print headers giving file names --help display this help and exit --version output version information and exit

And there you go.

And last, we'll print the first, say 220 bytes from the same file, using -c.

$ head -c 220 video27.txt Usage: head [OPTION]... [FILE]... Print the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name. With no FILE, or when FILE is -, read standard input.

There we go, just the syntax for the command.

A Final Tip

Okay now you know how to use head. And you know the syntax for commands, options and arguments.

One last tip about the head command. Well, head is a quick way to review the structure of a file, right? Oh, and here's a clue about our next video, if there's a head then there has to be a blank.

Okay, thanks for visiting today. I hope this was a helpful introduction to the head command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

Learn faster by pairing these web-based tutorials with videos, subscribe at YouTube and follow @factorpad on Twitter for updates.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / head-command.html


linux head command
head linux
linux head
head command
unix head
print first lines
print first bytes
display first lines
head command syntax
head command summary
linux head options
linux head tutorial
linux head video

A newly-updated free resource. Connect and refer a friend today.