shuf Linux Command Guide
The shuf command in Linux is used to generate random permutations of input lines. It can shuffle the contents of a file or produce a random sample from a list. By default, shuf outputs a random permutation of the input. Users can specify the number of output lines using the -n option. Additional options allow users to set a seed for the random generator, specify a range of randomizations, and more. This command is useful for randomizing the order of lines in a file or selecting random items from a list.
shuf Syntax:
Options:
Option | Description |
---|---|
-e | Treat each argument as an input line |
-i | Generate random numbers within a specified range |
-n | Output at most N lines |
-r | Allow repeated lines in the output |
-z | Delimit items with a NUL (‘\0’) byte |
Parameters:
Parameter | Description |
---|---|
input_file | Specify the input file. If not provided, uses stdin |
shuf Command Samples:
Shuffle Lines in a File
Randomizes the order of lines in the file.txt.
Shuffle a List of Numbers
Randomly shuffles the list of numbers 1, 2, 3, 4, and 5.
Generate a Random Permutation
Produces a random permutation of the elements a, b, c, d, and e.
Shuffle Lines and Output Only N Lines
Shuffles the lines in file.txt and prints only 3 lines.
Shuffle and Pick Randomly From a List
Shuffles the fruits list and selects 2 random items.
Shuffle and Redirect to a New File
Randomly shuffles the lines in names.txt and saves the output to shuffled_names.txt.
Shuffle and Limit Randomization by Seed
Shuffles file.txt using /dev/urandom as the seed and selects 4 random lines.
shuf FAQ:
How do I use shuf in Linux?
To use the shuf command in Linux, execute the following command:
What is the purpose of shuf in Linux?
The purpose of the shuf command in Linux is to generate random permutations of input lines.
How can I shuffle multiple files with shuf?
You can shuffle multiple files using shuf by providing them as arguments to the command. Here is an example:
Can I limit the number of lines shuffled with shuf in Linux?
Yes, you can limit the number of lines shuffled with shuf by using the -n
or --head-count
option followed by the number of lines. Here is an example:
How can I shuffle lines and then select a specific number of lines with shuf?
To shuffle lines and then select a specific number of lines with shuf, you can combine the -n
option with the command. Here is an example:
Is there a way to avoid repeating shuffled lines in shuf?
Yes, you can prevent shuf from repeating shuffled lines by using the -r
or --repeat
option. This will shuffle lines and print them in a random order without repeating any line until all lines have been printed at least once.
Applications of the shuf command
- Generating randomized quiz questions
- Shuffling the order of lines in a text file
- Randomly selecting winners in a contest
- Creating shuffled playlists
- Generating random test data for software testing