shuf
Randomly shuffle lines in a file
TLDR
Randomize the order of lines in a file and output the result
Only output the first 5 entries of the result
Write the output to another file
Generate 3 random numbers in the range 1-10 (inclusive)
SYNOPSIS
shuf [OPTION]... [FILE]
OR
shuf -e [OPTION]... [ARG]...
OR
shuf -i LO-HI [OPTION]...
PARAMETERS
-e, --echo
Treat each argument as an input line to be shuffled.
-i LO-HI, --input-range=LO-HI
Generate numbers in the specified range LO to HI (inclusive) as input lines.
-n COUNT, --head-count=COUNT
Output at most COUNT lines from the shuffled input.
-o FILE, --output=FILE
Write output to FILE instead of standard output.
-r FILE, --random-source=FILE
Get random bytes from FILE (e.g., /dev/urandom) instead of the default source.
-z, --zero-terminated
Line delimiter is NUL, not newline. Output lines are also NUL-terminated.
--help
Display a help message and exit.
--version
Output version information and exit.
DESCRIPTION
shuf is a command-line utility designed to generate a random permutation of its input lines. It can read lines from a specified file, from standard input, or generate a sequence of numbers within a given range. This makes it incredibly useful for tasks such as randomly selecting items from a list, shuffling data for experiments or simulations, or creating random test cases. By default, shuf outputs all shuffled lines to standard output. However, its behavior can be modified to limit the number of output lines using the --head-count option or to direct the output to a specific file. Its straightforward purpose and efficiency in handling random permutations make it a versatile tool for various scripting and data manipulation scenarios in the Linux environment.
CAVEATS
shuf loads all input into memory before performing the shuffle. For very large input files or streams, this can lead to significant memory consumption.
The randomness generated by shuf, by default, comes from the system's pseudo-random number generator (usually /dev/urandom). While generally sufficient for most practical purposes, it is not considered cryptographically secure without explicitly specifying a strong, truly random source using --random-source.
INPUT HANDLING
When no FILE is specified, or when FILE is specified as '-', shuf reads from standard input. If a FILE is provided, it reads from that file. When the -e option is used, all subsequent command-line arguments are treated as individual input lines. The -i option completely bypasses file or standard input, instead generating a sequence of numbers as its input.
RANDOMNESS SOURCE
By default, shuf utilizes the operating system's default source of pseudo-random numbers, typically /dev/urandom. The --random-source option offers flexibility by allowing users to specify an alternative file or device from which to read random bytes. This can be beneficial for scenarios requiring reproducible shuffles (by providing a fixed seed file) or when a higher quality of randomness is available from a different source.
HISTORY
shuf is a utility that is part of the GNU Coreutils package, a collection of essential tools found on most GNU/Linux systems. It was developed to provide a dedicated, standard, and efficient command for randomly permuting lines of text. Prior to its widespread adoption, similar functionality might have been achieved with less portable or more complex combinations of other commands (like GNU sort -R or custom scripts), making shuf a welcome addition that standardized this common operation.