How to use tail and head for log analysis
· Category: Linux
Short answer
head shows the start of a file; tail shows the end and can follow files in real time.
Steps
- View first 10 lines:
head file.txt
- View last 20 lines:
tail -n 20 file.txt
- Follow a log in real time:
tail -f /var/log/syslog
- Combine:
cat file.txt | head -50 | tail -10
Tips
tail -Ffollows across log rotations.- Use
greppiped intotailto filter then view recent matches. headandtailwork on piped input, not just files.
Common issues
tail -fstops when the file is truncated; use-Ffor robust following.- Binary files may produce garbled output.