shuf
Randomly shuffle input lines
TLDR
Shuffle lines of a file
SYNOPSIS
shuf [-n count] [-i range] [-e args] [-r] [options] [file]
DESCRIPTION
shuf randomly permutes input lines. It reads input, shuffles the order, and outputs all lines in random sequence.
Without options, shuf outputs all input lines in random order. The -n option limits output to the first N lines after shuffling - effectively random sampling without replacement.
Input range mode (-i) generates sequential numbers and shuffles them. Combined with -n, this selects random numbers from a range. Useful for generating lottery numbers, random IDs, or sampling.
Echo mode (-e) shuffles command-line arguments instead of file lines. This enables shuffling small lists without creating temporary files.
Repeat mode (-r) enables sampling with replacement - the same line can appear multiple times in output. This is useful for bootstrap sampling or simulation.
The random source option ensures reproducible shuffling when given a deterministic source, useful for testing.
PARAMETERS
-n NUM, --head-count NUM
Output at most NUM lines.-i LO-HI, --input-range LO-HI
Generate numbers from LO to HI.-e, --echo
Treat arguments as input lines.-r, --repeat
Output can repeat (with replacement).-z, --zero-terminated
Use NUL as line delimiter.-o FILE, --output FILE
Write to file instead of stdout.--random-source FILE
Get random bytes from file.
CAVEATS
Loads entire input into memory. Very large files may cause memory issues. Default randomness is good but not cryptographic. Without -n, outputs entire input. Different from sort -R which may group identical lines.
HISTORY
shuf is part of GNU coreutils, providing command-line random shuffling. While Unix systems had various random line selection tools, shuf provides comprehensive shuffling with range generation and sampling options.
