The head command

output the first part of files 14 examples from the lessons of the IVski Linux Playground — try them in the terminal next to them.

Options in the examples

-n, --lines=[-]NUM
print the first NUM lines instead of the first 10; with the leading '-', print all but the last NUM lines of each file

Examples

head /etc/passwd

First 10 lines (default)

head -n 20 /etc/passwd

First 20 lines

head -5 /etc/passwd

First 5 lines

head -20 file.txt | tail -10
head -1 data.csv

Check CSV headers

cat /etc/passwd | cut -d: -f1 | sort | head -10
history | awk '{print $2}' | sort | uniq -c | sort -rn | head -10
top -b -n 1 | head -20

One snapshot, pipe-able

du -sh /var/* | sort -rh | head -10
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -3
awk '{print $1}' ~/logs/access.log | sort | uniq -c | sort -rn | head -3

Requests per address, most first

ps aux --sort=-%cpu | head -4

The header and the 3 busiest processes

du -sh /* 2>/dev/null | sort -rh | head

Top-level space hogs, sorted

ps aux --sort=-%cpu | head

Top CPU-consuming processes

Lessons that use head

Related commands