If you ever played the game of cards, you'd likely be aware of the term shuffling. A bit hard to imagine, there's a Linux command line tool that exactly does that with lines in files. In this tutorial, we will discuss the basics of the 'shuf' command using some easy to understand examples.

But before we do that, it's worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.

Linux shuf command

The shuf command in Linux lets you generate random permutations. Following is its syntax:

shuf [OPTION]... [FILE]

And here's is how the tool's man page explains it:

Write a random permutation of the input lines to standard output.

Following are some Q&A styled examples that should give you a better idea on how the shuf command works.

Q1. How to use shuf command?

Basic usage is very simple. Just pass a file name as input. Here's an example:

shuf test.txt

Now, the test.txt file contains the following lines:

123456789

But the shuf command above produced the following output:

346518279

So you can see the lines got shuffled.

Q2. How to limit the number of lines in output?

If you want to limit the number of lines in shuf output, use the -n command line option, which requires you to pass a number that represents the maximum count of lines you want in output.

For example:

shuf -n 3 test.txt

The above command produced the following output:

649

Q3. How to allow repetition of lines in output?

Suppose the input file contains 9 lines, but you want shuf to produce more lines than that in output (using the -n option discussed above). This can be done using the -r option.

-r, --repeat output lines can be repeated

So if you want shuf to produce 25 lines in output, then run the following command:

shuf -r -n 25 test.txt

Here's the output this command produced:

6845933125759585288744853

Q4. How to make shuf treat input arguments as lines?

For this, use the -e command line option. Here's an example:

shuf -e 1 2 3 4 5 6 7 8 9

Here's the output produced by this command:

917862354

Conclusion

Agreed, shuf is not the kind of tool you'd require every day, but there's no harm in learning about this utility especially since its learning curve is not that steep. Once you're done practicing whatever we've discussed here, head to the tool's man page to know more about it.

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu's work has also been featured in some of world's other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.