Open In App

shuf Command in Linux with Examples

Last Updated : 10 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

shuf command in linux

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.

file shuf in linux

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.

to 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.

fule shuf command pied with seq command

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

list shuf in linux

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.

list shuf

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.

range shuf

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.



Next Article

Similar Reads