head and tail
For large files, you often just need the beginning or end.
head — First lines
head /etc/passwd # First 10 lines (default)
head -n 20 /etc/passwd # First 20 lines
head -5 /etc/passwd # First 5 lines
tail — Last lines
tail /var/log/syslog # Last 10 lines
tail -n 50 /var/log/syslog # Last 50 lines
tail -f /var/log/syslog # Follow: real-time updates!
The -f flag is incredibly useful for monitoring logs in real time.
Combining head and tail
# Print lines 10-20 of a file:
head -20 file.txt | tail -10
Practical uses
tail -f /var/log/nginx/access.log # Watch web server requests live
head -1 data.csv # Check CSV headers
tail -n +2 data.csv # Skip header line (print from line 2)