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
Shift command in Linux with examples
Shift is a built-in command in bash that after getting executed, shifts/moves the command line arguments to one position left. The first argument is lost after using the shift command. This command takes only one integer as an argument. This command is useful when you want to get rid of the command
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
w command in Linux with Examples
The 'w' command in Linux gives us important information about who is currently using the computer, how much the computer is being used, and what programs are running. It's a handy tool for people who take care of computer systems, as it helps them keep an eye on what users are doing, how much of the
3 min read
source Command in Linux with Examples
If you're new to the world of Linux, you might have heard about commands that do various tasks, but some like the 'source' command might seem a bit confusing at first. Don't worry; let's break it down step by step. What is the Source Command?The source command in Linux is like a magic wand that lets
7 min read
stty command in Linux with Examples
stty command in Linux is used to change and print terminal line settings. This command shows or changes terminal characteristics. Syntaxstty [-F DEVICE | --file=DEVICE] [SETTING]...stty [-F DEVICE | --file=DEVICE] [-a|--all]stty [-F DEVICE | --file=DEVICE] [-g|--save]where, -F DEVICE or --file=DEVIC
2 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
Stat command in Linux with examples
The 'stat' command is an invaluable tool in the Linux command suite, providing detailed statistics about files and file systems. It displays crucial information such as file size, permissions, ownership, and timestamps, making it an essential utility for system administrators and power users. What i
6 min read
until command in Linux with Examples
until command in Linux is used to execute a set of commands as long as the final command in the 'until' Commands has an exit status that is not zero. It is mostly used where the user needs to execute a set of commands until a condition is true. Syntaxuntil COMMANDS; do COMMANDS; donewhere, CONDITION
2 min read
users command in Linux with Examples
users command in Linux system is used to show the user names of users currently logged in to the current host. It will display who is currently logged in according to FILE. If the FILE is not specified, use "/var/run/utmp". "/var/log/wtmp" as FILE is common. Syntaxusers [OPTION]... [FILE]where, OPT
2 min read
lsmod command in Linux with Examples
lsmod command is used to display the status of modules in the Linux kernel. It results in a list of loaded modules. lsmod is a trivial program which nicely formats the contents of the /proc/modules, showing what kernel modules are currently loaded. Syntax: lsmod Example: Run lsmod at the command lin
1 min read