Pick Random Lines or Numbers with shuf

Shuffle input or draw N random picks from a file, an argument list, or a numeric range — the coreutils way to randomize anything.

LinuxshufrandomshellcliLow risk
Run shuf -n 3 names.txt to draw three random lines from a file, or shuf -i 1-100 -n 1 to roll a random number from 1 to 100.

01Command

Command
$ shuf -n 3 names.txt && shuf -e --repeat -n 5 A B C D E F && seq 1 100 | shuf -n 1

02What it does

shuf reads lines and outputs them in random order; -n limits the output to the first N random picks instead of the whole shuffled list. -e lets you randomize items typed directly on the command line, --repeat allows the same item to appear more than once (sampling with replacement), and -i 1-100 selects from a numeric range — a dice roll or lottery without a script. It composes naturally with other tools: seq 1 100 | shuf -n 1, shuf -n 5 songs.txt | mpv --playlist=- for a random five-song queue, or shuf -n 1 users.txt to draw a raffle winner.

03Risk

RiskLow risk
Requirementscoreutils (preinstalled on all Linux distributions)
!Warning: shuf shuffles lines, so multi-word entries are fine but entries containing newlines will be split — keep one item per line in your input file.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • shuf(1) — Linux manual page
    man7.org · community · 2026-09-17
    Evidence: shuf generates random permutations: -n outputs N lines, -e treats operands as items, --repeat allows picking the same item more than once, -i picks from an integer range.

Last verified: 2026-09-18 · Category: Developer