opscape

Text processing

shuf

Shuffle lines into random order, or pick random lines, numbers or words.

shuf [OPTION...] [FILE] | shuf -e [ARG...] | shuf -i LO-HI

9 of 9 examples run in Debian 13.7 on 2026-10-31 · coreutils 9.7-3 · manual: shuf(1)

shuf prints its input lines in a random order. With -n N it stops after N lines, which makes it a random sampler: one random line from a file, five random test cases, a random winner. It can also shuffle its arguments (-e) or a range of integers (-i 1-100) without any input file, and with -r it samples with replacement, so values can repeat, like dice rolls.

Every example on this page uses real randomness, so its output differs each time the page is built; that is the point of the tool. When you need a repeatable shuffle (a fixed train/test split, a reproducible test order), give shuf a fixed stream of bytes with --random-source: the same bytes always produce the same order.

shuf is a GNU coreutils program. sort -R also shuffles, but it keeps identical lines next to each other, so it is not a true shuffle.

Examples

Shuffle all lines

shuf todo.txt
explain
output
buy milk
fix login bug
deploy v2.4
write tests
fix logout bug

All five lines, in a random order that changes every run.

Pick random lines

shuf -n 2 todo.txt
explain
output
write tests
fix login bug

-n 2 outputs only two lines, picked at random without repeats.

A random line from a file

shuf -n 1 data/fruits.txt
explain
output
apple

One line chosen uniformly at random. apple appears three times in the file, so it is three times as likely as fig to be picked.

A random number in a range

shuf -i 1-6 -n 1
explain
output
2

-i 1-6 shuffles the integers 1 to 6 instead of reading input; -n 1 keeps one, like rolling a die.

Shuffle words given on the command line

shuf -e red green blue yellow
explain
output
blue
green
yellow
red

-e treats each argument as an input line, so no file or pipe is needed.

Sample with replacement

shuf -r -n 10 -e H T | paste -s -d ' '
explain
output
H H H T T T T H H H

-r lets values repeat, so this is ten independent coin tosses. Without -r, -n 10 could not return more than the two values that exist.

A repeatable shuffle

shuf --random-source=<(yes 42) -i 1-10 | paste -s -d ' '
shuf --random-source=<(yes 42) -i 1-10 | paste -s -d ' '
output
3 7 5 10 1 6 9 8 2 4
3 7 5 10 1 6 9 8 2 4

--random-source reads "random" bytes from a file instead of the system's generator. Here the endless output of yes 42 acts as a seed: both runs print the same order, and a different seed string gives a different one. This is for reproducibility, not security.

Split data into random halves

tail -n +2 data/users.csv | shuf --random-source=<(yes split) > shuffled.csv
head -n 4 shuffled.csv > train.csv
tail -n +5 shuffled.csv > test.csv
wc -l train.csv test.csv
output
  4 train.csv
  4 test.csv
  8 total

Shuffle once with a fixed seed, then cut the result: the 8 users are divided into two groups of 4, and rerunning gives the same groups.

Write the result to a file

shuf -n 3 -o picks.txt todo.txt
wc -l picks.txt
output
3 picks.txt

-o writes to a file, which may even be the input file, since shuf reads everything first.

Common pitfalls

  • sort -R is not a shuffle: it sorts by a random hash of each line, so duplicate lines always end up adjacent. Use shuf when duplicates must be spread out.

  • Without -n, shuf holds the whole input in memory before printing anything. With -n, GNU shuf can sample a large input without keeping all of it.

  • --random-source with a short file fails with an end-of-file error once the bytes run out. Use an endless stream such as <(yes seed).

  • shuf -i and -e cannot be combined with an input file.

GNU vs BSD and macOS

The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).

  • macOS and the BSDs have no shuf in the base system. Install GNU coreutils (where it may be named gshuf), or use sort -R on macOS, with the duplicate caveat above.

Options worth knowing

A selection, described in our own words. For the full list, see shuf(1) or shuf --help.

OptionWhat it does
-n, --head-count COUNTOutput at most COUNT lines.
-e, --echoTreat each command-line argument as an input line.
-i, --input-range LO-HIShuffle the integers from LO to HI.
-r, --repeatAllow output lines to repeat (sampling with replacement).
-o, --output FILEWrite the result to FILE.
--random-source FILEGet random bytes from FILE, for repeatable results.
-z, --zero-terminatedLines end with NUL instead of newline.

Related