head – output the first part of files

head command
head command

head displays the first lines of a file. By default, it shows the first 10 lines.

Synopsis

head [OPTIONS] [FILE...]

Common Options

OptionDescription
-n NShow first N lines
-c NShow first N bytes
-qQuiet, don’t print headers for multiple files
-vAlways print headers

Examples

Show first 10 lines (default)

$ head /var/log/syslog
Jan 15 00:00:01 server CRON[1234]: ...
Jan 15 00:00:02 server systemd[1]: ...
...

Show first N lines

$ head -n 5 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync

Show first N bytes

$ head -c 100 file.txt

Multiple files

$ head -n 3 file1.txt file2.txt
==> file1.txt <==
Line 1
Line 2
Line 3

==> file2.txt <==
First line
Second line
Third line

Combine with other commands

$ ls -lt | head -n 5          # 5 most recently modified
$ ps aux | head -n 10         # First 10 processes
$ history | head -n 20        # First 20 commands

Show all but last N lines

$ head -n -5 file.txt         # All lines except last 5

Tips

  • Quick file preview: head file.txt before opening large files
  • CSV headers: head -n 1 data.csv to see column names
  • Combine with tail: head -n 100 | tail -n 10 for lines 91-100
  • Watch growing files: Use tail -f instead for live updates

See Also

  • tail — Show last lines of file
  • cat — Show entire file
  • less — Interactive file viewer
  • more — Simple pager

Tutorials