LinuxCommandLibrary

shuf

Randomly shuffle lines in a file

TLDR

Randomize the order of lines in a file and output the result

$ shuf [path/to/file]
copy

Only output the first 5 entries of the result
$ shuf [[-n|--head-count]] 5 [path/to/file]
copy

Write the output to another file
$ shuf [path/to/input_file] [[-o|--output]] [path/to/output_file]
copy

Generate 3 random numbers in the range 1-10 (inclusive)
$ shuf [[-n|--head-count]] 3 [[-i|--input-range]] 1-10 [[-r|--repeat]]
copy

SYNOPSIS

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

PARAMETERS

-e, --echo [ARG]...
    Treat each ARG as an input line.

-i, --input-range=LO-HI
    Treat each number LO through HI as an input line.

-n, --head-count=COUNT
    Output at most COUNT lines.

-o, --output=FILE
    Write result to FILE instead of standard output.

-r, --repeat
    Output lines can be repeated.

-z, --zero-terminated
    Line delimiter is NUL, not newline.

--random-source=FILE
    Get randomness from FILE.

--help
    Display help message and exit.

--version
    Output version information and exit.

DESCRIPTION

The shuf command generates random permutations of input lines. It reads lines from the specified file (or standard input if no file is given) and outputs a random permutation of those lines to standard output. shuf is often used for sampling, randomizing lists, or creating random selections from a dataset. It is a versatile tool for tasks where randomness is required. It provides options to control the range of numbers to shuffle and the number of output lines. One common use case is to select a random subset of lines from a large file without reading the entire file into memory.
shuf can also generate a sequence of random integers within a specified range. The output can be tailored to fit specific requirements, such as limiting the number of output lines or repeating the output sequence indefinitely.

CAVEATS

The randomness provided by shuf depends on the underlying random number generator of the system. For security-sensitive applications, consider using a cryptographically secure random number generator. If the input file is very large, using the `-n` option is crucial to avoid excessive memory usage.

EXAMPLES

  • Shuffle lines from a file: shuf myfile.txt
  • Select 10 random lines from a file: shuf -n 10 myfile.txt
  • Generate 5 random numbers between 1 and 100: shuf -i 1-100 -n 5
  • Shuffle a list of arguments: shuf -e apple banana cherry

SEE ALSO

sort(1), head(1), tail(1), seq(1)

Copied to clipboard