Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Use the Linux head Command

Show the first lines (or bytes) of a file or of command output.

VPS and Cloud Servers2 min read9 screenshots

What head does

head prints the beginning of a file. By default it prints the first 10 lines. It is helpful for a quick look at big files, such as logs.

head [options] fileName

Options

OptionWhat it does
-nNumber of lines to show from the start
-cNumber of bytes to show from the start
-vAlways print the file name
-qNever print the file names (for several files)

Examples

These examples use a file called sampleFile.txt. First look at all of it:

cat sampleFile.txt
The sample file

1. Basic use

head sampleFile.txt
The first 10 lines

2. A number of lines with -n

head -n 7 sampleFile.txt
The first 7 lines

You can use head -n -3 to show everything except the last 3 lines.

3. A number of bytes with -c

head -c 14 sampleFile.txt
The first 14 bytes

4. Show the file name with -v

head -n 3 -v sampleFile.txt
Showing the file name

5. Several files

head -n 3 sampleFile.txt exampleFile.txt
The first lines of two files

head prints a header with the name of each file.

6. Hide the file names with -q

head -n 3 -q sampleFile.txt exampleFile.txt
Without file names

7. Save the result in a file

head -n 5 sampleFile.txt > sampleClone.txt
cat sampleClone.txt
Saving the output to a file

8. Use it in a pipe

ls -l | head -n 6
head in a pipe

Quick recap

  • head shows the first 10 lines by default.
  • -n sets lines, -c sets bytes.
  • Use it in pipes to limit long output. The partner command tail shows the end.