Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Text Processing

Linux shuf command

Reference for the Linux shuf command: shuffle lines, pick N random lines, generate random numbers in a range, make results repeatable with a random source.

shuf writes a random permutation of its input lines. With -n it picks that many lines, with -r it picks with repetition, and with -i it permutes a range of numbers without any input file. It is the standard way to shuffle a playlist, sample a dataset, or pick a random item.

The randomness comes from the system's random generator unless you supply a source, so ordinary output is different on every run. For examples and tests, give --random-source a fixed file to make the result repeatable.

Terminal
shuf [OPTION]... [FILE]

Try it

Bash Shuffle, sample and ranges (counts only)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Repeatable shuffles

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
shuf [OPTION]... [FILE]
shuf -e [OPTION]... [ARG]...
shuf -i LO-HI [OPTION]...
Option Description
-n N, --head-count=N Output at most N lines.
-r, --repeat Sample with replacement; lines may repeat, and the output never ends unless -n is given.
-e, --echo Treat each argument as an input line instead of reading a file.
-i LO-HI Use the integers from LO to HI as input lines.
-o FILE Write to FILE (works even if it is also the input).
--random-source=FILE Take random bytes from FILE rather than the system generator.
-z Lines end in NUL instead of newline.

Returns: 0 on success, non-zero on error (for example an invalid range).

Usage notes

  • Whole input is read first (except for -n with a large file, which uses a reservoir), so shuffling a huge file needs memory or temporary space.
  • Uniform: every ordering is equally likely. Sampling with -n takes the first N lines of such a shuffle, so every subset is equally probable.
  • Random numbers: shuf -i 1-6 -n 1 rolls a die. It is fine for games and sampling, not for secrets or tokens; use openssl rand or /dev/urandom for those.
  • $RANDOM versus shuf: Bash's $RANDOM is limited to 0-32767 and has modulo bias when used with %. shuf -i has no such limits.
  • Repeatable results: a file of fixed bytes as --random-source makes output identical on every run, which is how tests should use shuf. The file must be long enough, or shuf fails with an "end of file" error.
  • sort -R also randomizes, but identical lines are grouped together, and it is slower. Use shuf for a true shuffle.
  • Empty input produces empty output and exit status 0.
  • In-place shuffle: shuf file -o file is safe, but shuf file > file empties the file first.