Four ways to look, and one that lies
◈ 9 cardsPick the right viewer for a file of a given size and purpose; separate the last N lines from everything after line N; use head and tail as diagnostics rather than as viewers.
One fixture, five tools
Everything below runs against a single file, roster.txt, which holds exactly 26 numbered lines: line01 on the first line, line02 on the second, and so on down to line26. One fixture means every result is checkable by eye — if a command claims to have printed the last three lines, you can see whether line24, line25 and line26 came out.
cat roster.txt prints all 26 lines and returns you to the prompt. On a 26-line file that is the right tool. On a 26,000-line file it is useless: the lines stream past and the terminal keeps only the last screenful, so cat tells you what is at the end of the file while appearing to have shown you the whole thing. That is the tool that lies — not because it is broken, but because a terminal scrollback is not a reader.
more roster.txt shows one screenful and waits. <Space> gives you the next page, <Enter> the next single line, q quits, and the bottom-left corner tells you what percentage of the file you have seen. It never reads more of the file than it has shown you, so it opens instantly on any size of file.
less roster.txt does everything more does and adds the thing more cannot: backward motion. b goes back a page, G jumps to the end, g to the start, /pattern searches forward and ?pattern backward, and most of the vi Command-mode keys work. Like more, it starts displaying before it has read the file, which is why it opens a gigabyte-sized log in the time it takes vi to notice you asked.
Head and tail: the two ends, and the trap between them
head prints the first lines of a file — ten of them by default:
$ head roster.txt
line01
line02
...
line10
$ head -3 roster.txt
line01
line02
line03
tail prints the last lines, also ten by default, so tail roster.txt gives line17 through line26 and tail -3 roster.txt gives line24, line25, line26.
Now the trap, and it is the single most-tested fact in this section. tail accepts a plus form as well as a minus form, and they mean completely different things:
tail -3 roster.txt— the last three lines:line24 line25 line26.tail -n +3 roster.txt— everything from line 3 onward:line03throughline26, twenty-four lines.
The minus form counts backward from the end; the plus form counts forward from the start and never stops. The historic spelling of the second form is tail +3 roster.txt, which is what the textbook uses and what GNU tail still accepts, but the portable POSIX spelling is -n +3 and that is what you should write. A useful consequence: tail -n +1 roster.txt starts at line 1 and runs to the end, which is exactly cat roster.txt.
Worked example — head as a diagnostic
Head and tail are not only viewers. Give head a file you cannot identify and it will usually tell you what the file is, because most structured formats put their identity in the first few bytes. Suppose a classmate sends you report.ps and you want to know what it is before you print 400 pages of it:
$ head -5 report.ps
%!PS-Adobe-3.0
%%Creator: dvips(k) 5.997
%%BoundingBox: 0 0 612 792
%%Pages: 12
%%Orientation: Portrait
Five lines and you know four things: it is PostScript conforming to the 3.0 document-structuring convention; it was produced by dvips, so it began life as TeX; the page box is 612 by 792 points, which is US Letter; and it is twelve pages, portrait. You have learned all of that without opening the file in anything, and — crucially — without cat-ing a possibly-binary file at your terminal.
The other end confirms the diagnosis. tail -2 report.ps should show the %%EOF terminator; if it does not, the file was truncated in transit and printing it will fail halfway. Head and tail are how you interrogate a file you do not trust.
Choosing
The decision is almost mechanical. Shorter than a screen: cat. Longer, and you want to read it: less. You only want the start, or you want to identify the file: head. You only want the end, or you want to watch it grow: tail. more still works everywhere and is worth knowing because it is on every system, including ones so minimal that less is not installed.