How to View File Starts and Ends with head and tail
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.
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.
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.
Filter what you follow
Pipe tail -f into grep to watch only errors as they appear:
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.
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.