Linux shuf Command Tutorial for Beginners (with Examples)

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:

1
2
3
4
5
6
7
8
9

But the shuf command above produced the following output:

3
4
6
5
1
8
2
7
9

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:

6
4
9

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:

6
8
4
5
9
3
3
1
2
5
7
5
9
5
8
5
2
8
8
7
4
4
8
5
3

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:

9
1
7
8
6
2
3
5
4

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.

Share this page:

Suggested articles

0 Comment(s)

Add comment