Open In App

shuf Command in Linux with Examples

Last Updated : 02 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
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

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads