The `shuf` command in Unix/Linux-based systems is a command-line utility...
June 30, 2025 at 04:26 AM
The shuf
command in Unix/Linux-based systems is a command-line utility used to generate random permutations of input lines. It can randomly reorder lines from a file or from standard input, or even generate random numbers in a given range. Here's a breakdown of a general shuf
command functionality:
-
Random Line Reordering:
- If you have a file containing lines of text (e.g.,
file.txt
), runningshuf file.txt
will output the lines in a random order.
- If you have a file containing lines of text (e.g.,
-
Generate Random Numbers:
- Using the
-i
option, you can specify a range of numbers to randomize. For example,shuf -i 1-10
will generate a random permutation of numbers between 1 and 10.
- Using the
-
Selecting a Subset of Lines:
- With the
-n
option, you can specify how many lines or entries to output. For instance,shuf -n 3 file.txt
will randomly select three lines fromfile.txt
.
- With the
-
Write to Another File:
- The
-o
option allows saving the output to a given file. For example,shuf file.txt -o shuffled.txt
will write the shuffled lines into a new file calledshuffled.txt
.
- The
-
Random Seed:
- When using the
--random-source
or--seed
options, you can provide a seed to produce repeatable results (useful for testing).
- When using the
Example:
shuf -i 1-100 -n 5
This generates 5 random numbers in the range 1 to 100.
In summary, the shuf
command is a versatile tool for randomizing lines, generating random numbers, and selecting random subsets of entries either from files or a numerical range.
Generate your own explanations
Download our vscode extension
Read other generated explanations
Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node