What less must know to draw one screen
To draw the first screen, less needs the first lines of the file, enough to fill the terminal rows. It needs to know where each of those lines starts and ends, and it needs the file size so it can display the position indicator. It does not need the last line, the middle of the file, or any line beyond the screen. The design of less follows directly from that requirement. It reads on demand, and it remembers the byte offsets of the lines it has seen.
The pager keeps a small index of line starts, which grows as you scroll. Each entry is a byte offset, so the index is far smaller than the file. Because the offset is a number, less can jump to a line whose offset it knows, and it can reach the end by seeking to the size of the file.
The system calls behind the first screen
When less opens a regular file, it calls the open system call and asks for the file size with fstat. It then reads a block of data with read, which the kernel satisfies from the page cache or from disk. The size tells the pager where the end is. If you press G to jump to the end, less seeks near the end with lseek and reads backward to find a line boundary. It does not need to read the file from the beginning to reach the last line.
A call such as lseek moves the file position without reading any data. That cost is constant, regardless of how large the file is. A read of one block costs roughly the same whether the file is a megabyte or a terabyte. These two operations together are the reason the first screen is immediate.
Why a pipe behaves differently
When the input is a pipe, less cannot seek, because a pipe has no size and no position that can be set. The program must read the stream from the beginning and keep what it has read if you want to go back. For a large stream, this means it buffers as much as it needs in memory and possibly on disk. A command that produces output slowly will also make the pager wait, because the screen cannot be filled until enough data arrives.
This explains a practical difference. less on a file is instant, while the same content piped in may need a moment, and jumping to the end of a stream requires reading all of it first.
The page cache makes repeat reads cheap
The kernel keeps recently read file data in memory, in the page cache. When less reads a block that was read recently, the kernel copies it from memory instead of from disk. That is why scrolling back to a part you just read is quick, and why opening the same log twice is faster the second time. The cache is shared among all processes, so another program that reads the file also benefits.
The page cache is managed by the kernel, not by less. A program cannot delete it, and the kernel reclaims memory from it when another program needs the space. This is why memory use looks high on a busy system even when no program seems to be using much.
Searching a huge file
Searching is also lazy. less reads blocks in sequence from the current position and stops at the first match. A search that hits the first screen is fast, and a search that must scan the whole file takes time proportional to the size of the file. The cost is in reading, not in the search itself, which is why patterns that match early feel instant.
What to take from this
A pager is fast on files because it reads only what the screen needs and seeks to what you ask for. When you work with a large file, prefer tools that seek, such as tail and head, for a fixed part of the file, and use less when you want to browse. If a file lives on a slow network filesystem, the same design still helps, because less reads only the blocks it shows.
You can see the system calls yourself with strace. Trace only the calls related to opening and seeking, and notice how few reads happen before the first screen is ready.
cd /workspace
seq 1 100000 > big.txt
strace -e trace=openat,lseek,read -o trace.txt less -F big.txt < /dev/null
wc -l trace.txtThe trace file lists the calls. The exact count depends on the environment, but the number of reads is small compared with the size of the file.
What to check when a large file is slow
A pager is fast for opening a file, but some operations take time. Asking for line numbers in the display can require less to count newlines from the start of the file, which means reading the file. Searching for a pattern that does not appear reads to the end. Jumping to the end with G reads backward from the last block, which is quick, while a search with a pattern that matches rarely is slow because it must examine almost everything.
When something is slow, ask which operation you requested. If you only need the last part of a log, tail is faster than less, because it never needs the start of the file. If you need one matching region, grep with a few lines of context often answers the question before a pager is opened at all. Match the tool to the question, and the file size stops being a problem.
Files on network filesystems behave differently from local disks, because every block read may cross the network. In that case, reading fewer blocks matters more, and a pager that reads only what it displays is still the better choice than a tool that loads the whole file into memory.