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.
shuf [OPTION]... [FILE]
Try it
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.
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
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
-nwith 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
-ntakes the firstNlines of such a shuffle, so every subset is equally probable. - Random numbers:
shuf -i 1-6 -n 1rolls a die. It is fine for games and sampling, not for secrets or tokens; useopenssl randor/dev/urandomfor those. $RANDOMversusshuf: Bash's$RANDOMis limited to 0-32767 and has modulo bias when used with%.shuf -ihas no such limits.- Repeatable results: a file of fixed bytes as
--random-sourcemakes output identical on every run, which is how tests should useshuf. The file must be long enough, orshuffails with an "end of file" error. sort -Ralso randomizes, but identical lines are grouped together, and it is slower. Useshuffor a true shuffle.- Empty input produces empty output and exit status 0.
- In-place shuffle:
shuf file -o fileis safe, butshuf file > fileempties the file first.
Related
sort,seq,head,wc- Lessons: Text pipelines, Text processing recipes