/ factorpad.com / tech / linux-essentials / head-command.html
An ad-free and cookie-free website.
Beginner
Learn to print the top part of a file at the Linux command line.
Videos can also be accessed from the Linux Essentials Playlist on YouTube.
Linux head Command Summary with Examples (3:38)
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.
-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.
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
.
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.
Then let's cat
that to the screen.
Too much to look at, huh? So let's head
the first 17 (lines) using -n 17
the
file video27.txt
.
And there you go.
And last, we'll print the first, say 220 bytes from the same file,
using -c
.
There we go, just the syntax for the command.
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.
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
Learn faster by pairing these web-based tutorials with videos, subscribe at YouTube and follow @factorpad on Twitter for updates.
wc
command, hit
Back.tail
command, click Next./ factorpad.com / tech / linux-essentials / head-command.html
A newly-updated free resource. Connect and refer a friend today.