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/passwdFirst 10 lines (default)
head -n 20 /etc/passwdFirst 20 lines
head -5 /etc/passwdFirst 5 lines
head -20 file.txt | tail -10
head -1 data.csvCheck 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 -20One 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 -3Requests per address, most first
ps aux --sort=-%cpu | head -4The header and the 3 busiest processes
du -sh /* 2>/dev/null | sort -rh | headTop-level space hogs, sorted
ps aux --sort=-%cpu | headTop CPU-consuming processes
Lessons that use head
- head & tail — First and Last Lines
- Pipes & Redirection
- sort & uniq — Sorting and Deduplication
- top & htop — System Monitor
- df & du — Disk Usage
- Reading the Logs
- Project: Daily Log Report
- Project: Server Health Check
- Mission: Disk Full
- Mission: Security Sweep