SHUF(1) General Commands Manual SHUF(1)

shufprint contents shuffled

shuf [-z] [-n num] [file...]

shuf -e [-z] [-n num] [string...]

In it's first form, shuf reads lines from each file or if unspecified or when file is "-", lines are read from standard input. And are then shuffled and printed using a reservoir shuffle, see SHUFFLING for details.

In it's second form, shuf uses a Fisher-Yates shuffle to swap-shuffle all the strings, and then prints them out as lines.

Use each string as an input line.
num
Output at most num lines.
Use NULL as line delimiter, not newline.

In it's first form, shuf uses a reservoir of 512 lines. It picks a random location, prints a line if present, then inserts a newly read line. Once all lines are read it prints the lines still present in the reservoir.
While this isn't truly a random sort as lines beyond 512 won't be printed first, it allows to use a bounded amount of memory.

The shuf utility exits 0 on success, and >0 if an error occurs.

sort(1)

An shuf utility appeared in GNU coreutils 6.0.

Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>

October 31, 2025 Linux