Linux command list

head command: show the beginning of a file

__ __ _ _____
\ \ / /__| |_|_ _|__ _ __ _ __ ___
\ \ /\ / / _ \ '_ \| |/ _ \ '__| '_ ` _ \
\ V V / __/ |_) | | __/ | | | | | |
\_/\_/ \___|_.__/|_|\___|_| |_| |_| |_
 
A sandbox for trying head. Nothing here can touch your real files.
user@webterm:~/project$
 

head prints just the start of a file. Learn the default of ten lines, how to set the count with -n, and what happens with several files, by running the commands in a real terminal in your browser.

Updated: 2026-09-05

Syntax

head [options] file...

Prints the beginning of a file. Without a count, that means ten lines.

Try it first

The terminal on this page has a log file with 30 lines in it.

$ head logs/app.log
line 01
line 02
line 03
line 04
line 05
line 06
line 07
line 08
line 09
line 10

Thirty lines in the file, ten on your screen. That is the default.

Choosing how many lines

-n takes the count.

$ head -n 3 logs/app.log
line 01
line 02
line 03

This is the everyday way to look at a file with a header row.

$ head -n 2 users.csv
id,name
1,ada

That is usually enough to know what you are dealing with.

Several files at once

Pass more than one and each gets a heading.

$ head -n 2 users.csv notes.txt
==> users.csv <==
id,name
1,ada

==> notes.txt <==
buy milk
call the bank

When you actually reach for it

head is how you check the shape of something without reading all of it.

SituationWhat you type
Check the column names of a CSVhead -n 1 users.csv
See how a log startshead -n 20 app.log
Peek at the top of a long listingls -l | head
Guess a file formathead -c 100 data.bin

-c cuts by bytes rather than lines.

$ head -c 20 logs/app.log
line 01
line 02
line

Things that trip people up

head for the oldest, tail for the newest. Logs grow downwards, so what is happening right now is at the end. Reach for head only when you care about how something started.

The number is a count, not a line number. head -n 3 means the first three lines, not the third line.

Order matters around a pipe. head file | grep x searches the beginning of the file, while grep x file | head shows the first of the matches. Which one you want depends on what you are narrowing.

Practise it hands-on

webterm.appthis site

  • Advanced Terminal Commands

    Learn commands for specific situations

    Try the tutorial

learn.webterm.appa separate site

>_WEBTERM LEARN

WebTerm Learn: from one command to actually using it

Commands stick when they show up in a real sequence of work, not one at a time. There is a course that builds them up in order.

See the course

Related commands

tail / less / cat / wc

Frequently asked questions

How many lines does head print?
Ten by default. Use -n to ask for a different number.
When should I use head instead of cat?
cat prints everything, so a long file scrolls away. When you only need to see the shape of the contents, head is quicker and safer.
How do I see the end of a file?
Use tail. head takes the beginning, tail takes the end, and the options work the same way.
Can I pipe another command into head?
Yes. ls -l | head shows just the top of a long listing.
Can it cut by characters instead of lines?
-c takes a number of bytes instead, which may stop in the middle of a line.