Bash View End of File
tail prints the last lines of a file and, with -f, keeps the terminal open to show new lines as they're written — the standard way to watch a live log.
Printing the End of a File
tail is the mirror image of head: instead of the beginning, it shows the last lines of a file, 10 by default. It's the natural tool for checking the most recent entries in a log without scrolling through the whole thing.
Default: last 10 lines
$ tail server.log
2026-07-16 09:58:01 INFO request handled in 42ms
2026-07-16 09:58:03 INFO request handled in 38ms
2026-07-16 09:58:05 WARN slow query took 1.2s
2026-07-16 09:58:06 INFO request handled in 40msChoosing a Line Count with -n
Just like head, tail accepts -n followed by a number to control how many lines from the end are shown. A plus sign before the number changes the meaning: -n +5 prints starting from line 5 all the way to the end of the file.
Show only the last 3 lines
$ tail -n 3 server.log
2026-07-16 09:58:03 INFO request handled in 38ms
2026-07-16 09:58:05 WARN slow query took 1.2s
2026-07-16 09:58:06 INFO request handled in 40msFollowing a Growing File with -f
The -f flag (follow) makes tail keep running after printing the last lines, printing each new line the instant it's appended to the file. This is the standard way to watch a live application or server log while you reproduce a bug. Press Ctrl+C to stop following.
Follow a log file live
$ tail -f server.log
2026-07-16 10:02:11 INFO request handled in 35ms
2026-07-16 10:02:14 INFO new connection from 10.0.0.4
2026-07-16 10:02:16 ERROR database timeout after 5000ms
^C- tail file — print the last 10 lines
- tail -n 50 file — print the last 50 lines
- tail -n +5 file — print from line 5 to the end
- tail -f file — follow the file and print new lines as they arrive
- tail -f -n 20 file — show the last 20 lines, then follow
Exercise: Bash Viewing Files
What is the main difference between "cat" and "less" when viewing a large file?