shuf Command in Linux with Examples
Last Updated :
10 Oct, 2024
The shuf command in Linux writes a random permutation of the input lines to standard output. It pseudo randomizes an input in the same way as the cards are shuffled. It is a part of GNU Coreutils and is not a part of POSIX. This command reads either from a file or standard input in bash and randomizes those input lines and displays the output. Like other Linux commands, shuf command comes with --help option.
Syntax
shuf [OPTION] [FILE] //file shuf
shuf -i LO-HI [OPTION] // range shuf
shuf -e [OPTION]... [ARG] //list shuf
where,
- OPTION: Various flags can be used to customize the output.
- FILE: The name of the file from which to read input. If omitted, the command reads from standard input.
- LO-HI: Specifies a range of integers for randomization.
- ARG: Individual elements to shuffle when using list shuffling.
Using the shuf Command
1. shuf command without any option or argument
shuf
When shuf command is used without any argument in the command line, it takes input from the user until CTRL- D is entered to terminate the set of inputs. It displays the input lines in a shuffled form. If 1, 2, and 3 are entered as input lines, then it generates 1.2 and 3 in random order in the output as seen in the image below:

Ways of using shuf command
1. File shuf
shuf [option] [file]
When shuf command is used in the above form i.e, without -e or -i option, then it operates as a file shuf i.e, it shuffles the contents of the file. The file_name is the last parameter of the shuf command and if it is not given, then input has to be provided from the shell or pipe.
Consider an example where input is taken from a file:
shuf file.txt
Suppose file.txt contains 6 lines, then the shuf command displays the input lines in random order as output.

Any number of lines can be randomized by using -n option.
shuf -n 2 file.txt
This will display any two random lines from the file.

Consider an example where Input is taken from the pipe:
{
seq 5 | shuf
}
seq 5 returns the integers sequentially from 1 to 5 while the shuf command takes it as input and shuffles the content i.e, the integers from 1 to 5. Hence, 1 to 5 is displayed as output in random order.

2. List shuf
shuf -e [OPTION]... [ARG]
When -e option is used with shuf command, it works as a list shuf. The arguments of the command are taken as the input line for the shuf.
Consider an example:
shuf -e A B C D E
It will take A, B, C, D, E as input lines, and will shuffle them to display the output

Any number of input lines can be displayed using the -n option along with -e option.
shuf -e -n 1 A B C D E
This will display any one of the inputs.

3. Range shuf
shuf -i LO-HI [OPTION]
When -i option is used along with shuf command, it acts as a range shuf. It requires a range of input as input where L0 is the lower bound while HI is the upper bound. It displays integers from L0-HI in shuffled form.

Conclusion
The shuf command is a versatile tool for randomizing lines, generating random numbers, and sampling input in Linux. By mastering its various options, you can automate tasks that involve randomization or create test data for scripts. Whether you’re a developer, system administrator, or a data scientist, the shuf command can help simplify tasks that require randomness.
Similar Reads
lshw command in Linux with Examples The 'lshw' (List Hardware) command in Linux/Unix is a powerful tool for extracting detailed information about the system's hardware configuration. This tool retrieves data from files in the '/proc' directory and is capable of reporting on a wide array of components, including memory configuration, C
3 min read
seq command in Linux with Examples The 'seq' command in Linux is a powerful utility used to generate a sequence of numbers. It is particularly useful in scenarios where you need to create a list of numbers within loops, such as while, for, or until loops. With 'seq', you can quickly generate numbers from a starting value (FIRST) to a
4 min read
shred Command in Linux with Examples When you delete a file from Linux or from any Operating System, then the file is not deleted permanently from the hard disk. When a file is deleted, it first gets moved to the trash and as soon as you clear off the trash the files get deleted for the file system. But the file is still there on your
6 min read
ln command in Linux with Examples The 'ln' command in Linux is a powerful utility that allows you to create links between files. These links can either be hard links or soft (symbolic) links. If you're unfamiliar with these concepts, check out our detailed guide on Hard and Soft Links in Linux to understand their differences, use ca
3 min read
uname command in Linux with Examples Linux, renowned for its open-source flexibility and powerful performance, offers a range of commands that reveal the inner workings of your system. Among these, the 'uname' command stands out as a versatile tool that provides key details about your Linux machine. Here, we will learn the basics of th
4 min read
od command in Linux with example The od (octal dump) command in Linux is a versatile tool used to display file contents in various formats, with the default being octal. This command is particularly useful for debugging scripts, examining binary files, or visualizing non-human-readable data like executable code. It allows users to
6 min read