How to View File Starts and Ends with head and tail

Published 2026-08-05 · Learn Linux

The head and tail commands print the first and last lines of a file. head -n 20 app.log shows the first 20 lines, and tail -n 20 app.log shows the last 20. The killer feature is tail -f, which follows a file as it grows — the standard way to watch logs.

Show the first lines with head

By default head prints the first ten lines. Pick a different count with -n.

$ head -n 20 README.md

Show the last lines with tail

tail prints the last ten lines by default. Use -n for a different count, which is how you read the end of a long log without opening it.

$ tail -n 50 app.log

Follow a file as it grows with -f

The -f flag keeps tail running and prints new lines as they are written. This is the classic way to watch an application log live. Press Ctrl+C to stop.

$ tail -f /var/log/nginx/access.log

Filter what you follow

Pipe tail -f into grep to watch only errors as they appear:

$ tail -f app.log | grep -i error

Head and tail in one command

To see a slice from the middle of a file, combine them: the first command takes the first 200 lines, the second takes the last 50 of those.

$ head -n 200 data.txt | tail -n 50

See head and tail for the full references.

Keep learning

Related guides: How to View Files with cat, less and more · How to Search Files for Text with the Linux grep Command · How to Count and Cut Text with wc and cut. Read all tutorials on the Learn Linux blog.