How do head and tail work for viewing file portions?

Answer

head file prints the first 10 lines of a file. Use head -n 50 file to specify the number of lines. tail file prints the last 10 lines. tail -n 100 file shows the last 100 lines. The most important option is tail -f file which follows the file — it continuously outputs new lines as they are appended, making it essential for monitoring live log files: tail -f /var/log/nginx/access.log. Combine tail -f with grep: tail -f app.log | grep ERROR. head and tail can also be chained to extract a range: head -100 file | tail -50 extracts lines 51–100.