Home·Terminal commands

shuf

Text processing

Usage

shuf [file]

Prints its input lines in random order; it is GNU coreutils and absent on macOS, where the usual stand-in sort -R groups equal lines together.

Common flags

FlagMeaning
-nPrint only N lines
-eTreat the arguments themselves as the lines
-iDraw from a number range, as in -i 1-100
-rAllow the same line to come out more than once
-oWrite to a file instead of standard output
--random-sourceUse a fixed source so the shuffle can be repeated

Examples

shuf -n 1 names.txt

Picks one line at random.

shuf -i 1-49 -n 6

Six different numbers between 1 and 49.

sort -R names.txt

The nearest thing on a machine without shuf.

These were built to be piped together. Three joined commands are usually shorter than one that does everything.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does shuf do?

Prints its input lines in random order; it is GNU coreutils and absent on macOS, where the usual stand-in sort -R groups equal lines together.

Q. How do I type it?

shuf [file] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

6 are listed here; the full set is in man shuf. This command sits under Text processing.

Related commands

man page: man shuf