Limiting a pipe's output does not limit the work the pipe does. Each stage produces its full result before the next stage runs, so a later stage that uses only part of an earlier stage's output still pays for that earlier stage in full.
For example, a Find piped into Head(1) over a large directory walks the whole tree before Head runs: Find produces every file under the starting path, and only then does Head take the first. On a home directory with hundreds of thousands of files, returning one item takes tens of seconds to minutes, or runs out of memory. The same shape in the shell, find ~ | head -1, returns almost immediately, because head takes one line and the pipeline stops.
To reproduce: run a Find over a large tree into Head(1), and compare the wall-clock time against find <same path> | head -1.
Limiting a pipe's output does not limit the work the pipe does. Each stage produces its full result before the next stage runs, so a later stage that uses only part of an earlier stage's output still pays for that earlier stage in full.
For example, a
Findpiped intoHead(1)over a large directory walks the whole tree beforeHeadruns:Findproduces every file under the starting path, and only then doesHeadtake the first. On a home directory with hundreds of thousands of files, returning one item takes tens of seconds to minutes, or runs out of memory. The same shape in the shell,find ~ | head -1, returns almost immediately, becauseheadtakes one line and the pipeline stops.To reproduce: run a
Findover a large tree intoHead(1), and compare the wall-clock time againstfind <same path> | head -1.