Memra

Four ways to look, and one that lies

◈ 9 cards

Pick 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.txteverything from line 3 onward: line03 through line26, 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.

ToolMust reach end offile?Scroll backward?Reach for itwhen...catyes - it prints allof itnothe file is shorterthan a screenmoreno - stops whereyou stopnoyou want a quickforward readlessno - opensinstantlyyesthe file is big, oryou must search itheadno - stops after Nlinesn/ayou want the firstlines, or the filetypetailyes - the end isthe pointn/ayou want the lastlines, or -f on alogcat on a long file shows you only the last screenful.
The middle two columns are why `less` beats `more` on a big file and why `head` is instant on a huge one: neither of them has to reach the end to give you an answer.
NORMAL ~/memra/learn/comp-325/viewing-files-cat-more-less-head-tail utf-8 LF