The shuf
command is a Linux utility that generates a random permutation from a given input. It is a simple command-line tool that can be used to randomize the order of lines in a file or generate a random sequence of numbers.
Overview
The basic syntax of the shuf
command is as follows:
shuf [OPTION]... [FILE]
Here, OPTION
refers to the various command-line options that can be used with the command, and FILE
refers to the input file from which the random permutation is to be generated. If no input file is specified, shuf
reads from standard input.
Examples
- Generate a random permutation of numbers from 1 to 10:
shuf -i 1-10
Output:
7
1
2
9
10
3
4
5
6
8
- Generate a random permutation of lines in a file:
shuf file.txt
Output:
line 3
line 1
line 4
line 2
- Generate a random permutation of lines in a file and save it to another file:
shuf file.txt > shuffled.txt
Specific use cases
- Randomizing the order of lines in a file
- Generating a random sequence of numbers for testing or simulation purposes
- Creating randomized lists or orderings
Options
The following table lists the available options for the shuf
command:
Option | Description |
---|---|
-e, --echo |
Treat each argument as an input line |
-i, --input-range=LO-HI |
Treat each number in the range LO through HI as an input line |
-n, --head-count=COUNT |
Output at most COUNT lines |
-o, --output=FILE |
Write output to FILE instead of standard output |
-r, --repeat |
Output lines can be repeated |
-z, --zero-terminated |
Line delimiter is NUL, not newline |
Troubleshooting tips
- If the input file is not found, make sure the correct path is specified.
- If the output is not what you expected, check the options used and make sure they are correct.
Notes
- The
shuf
command is available on most Linux distributions and can be installed using the package manager if not already installed. - The
shuf
command can be used in combination with other commands to create more complex operations.