Home·Terminal commands

head

Files and directories

Usage

head -n 20 [file]

Prints the start of a file; give it several files and it inserts a header for each unless you pass -q.

Common flags

FlagMeaning
-nPrint the first N lines
-cPrint the first N bytes
-qNever print the file name header
-vAlways print the file name header
-n -5GNU: everything except the last 5 lines

Examples

head -n 5 data.csv

Header row plus four records.

head -c 100 file.bin

The first 100 bytes only.

Moving, deleting and finding. Some of these do not ask twice, which is why -i becomes a habit.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does head do?

Prints the start of a file; give it several files and it inserts a header for each unless you pass -q.

Q. How do I type it?

head -n 20 [file] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

5 are listed here; the full set is in man head. This command sits under Files and directories.

Related commands

man page: man head