Robert Elder Software Inc.
  • Home
  • Store
  • Blog
  • Contact
  • Home
  • Store
  • Blog
  • Contact
  • #linux
  • |
  • #commandline
  • |
  • #softwareengineering
  • |
  • #compilers
  • |
  • #embeddedsystems
  • ...
  • View All >>

Intro To 'shuf' Command In Linux

2023-06-06 - By Robert Elder

     I use the 'shuf' command to generate random permutations of lines in a file:

shuf my-file.txt

Randomize Lines In A File

     Here, I have a file called 'students.txt' that contains a sorted list of students in a class:

Charles
Christopher
David
Elizabeth
Jennifer
Jessica
Karen
Linda
Lisa
Mary
Michael
Patricia
Richard
Susan
William

     I want to randomly rearrange the lines in this file to provide a seating assignment for each student.  I can use the 'shuf' command to generate a new randomly rearranged list like this:

cat students.txt | shuf
Michael
Karen
David
Susan
Patricia
Mary
Lisa
Elizabeth
Richard
Linda
Jennifer
Charles
Christopher
Jessica
William

Select N Random Lines From A File Without Replacement

     If I want to randomly select only three of the students from this list I can use the '-n' flag like this:

cat students.txt | shuf -n 3
Jennifer
Jessica
Karen

Select N Random Lines From A File With Replacement

     By default, the 'shuf' command prints out each line only once.  If you use the '-r' flag, all of the lines will be repeated an infinite number of times randomly so that duplicate lines can occur:

cat students.txt | shuf -r
Jessica
Michael
Michael
William
Richard
... Lines printed until you use Ctrl + C...

     Using the 'head' command I can limit the output to the first 10 lines:

cat students.txt | shuf -r | head -n 10
Susan
Richard
Richard
Michael
Michael
Patricia
Karen
Michael
Jessica
Elizabeth

     Notice how the output now contains duplicate lines (in this case, 'Richard' and 'Michael').

     For another example use of the '-r' flag, I can create 15 random samples from the file 'key-strokes.txt':

KEY_UP
KEY_DOWN
KEY_LEFT
KEY_RIGHT

     using the following command:

cat key-strokes.txt | shuf -r | head -n 15
KEY_LEFT
KEY_LEFT
KEY_DOWN
KEY_RIGHT
KEY_UP
KEY_DOWN
KEY_UP
KEY_RIGHT
KEY_LEFT
KEY_LEFT
KEY_UP
KEY_DOWN
KEY_UP
KEY_LEFT
KEY_DOWN

Generate Random Numbers Filling A Range

     I can also use the '-i' flag to specify a number range and the output will produce random numbers filling this range:

shuf -i 1-10
9
3
5
6
10
1
7
2
8
4
shuf -i 92-106
96
105
98
93
94
104
103
106
92
102
95
101
100
97
99

     And that's why the 'shuf' command is my favourite Linux command.

A Surprisingly Common Mistake Involving Wildcards & The Find Command
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21
Regular Expression Laptop Stickers
$20.00 CAD
Regular Expression Laptop Stickers
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01
The Most Confusing Grep Mistakes I've Ever Made
The Most Confusing Grep Mistakes I've Ever Made
Published 2020-11-02
Use The 'tail' Command To Monitor Everything
Use The 'tail' Command To Monitor Everything
Published 2021-04-08
Using A Piece Of Paper As A Display Terminal - ed Vs. vim
Using A Piece Of Paper As A Display Terminal - ed Vs. vim
Published 2020-10-05
An Introduction To Data Science On The Linux Command Line
An Introduction To Data Science On The Linux Command Line
Published 2019-10-16
An Overview of How to Do Everything with Raspberry Pi Cameras
An Overview of How to Do Everything with Raspberry Pi Cameras
Published 2019-05-28
Join My Mailing List
Privacy Policy
Why Bother Subscribing?
  • Free Software/Engineering Content. I publish all of my educational content publicly for free so everybody can make use of it.  Why bother signing up for a paid 'course', when you can just sign up for this email list?
  • Read about cool new products that I'm building. How do I make money? Glad you asked!  You'll get some emails with examples of things that I sell.  You might even get some business ideas of your own :)
  • People actually like this email list. I know that sounds crazy, because who actually subscribes to email lists these days, right?  Well, some do, and if you end up not liking it, I give you permission to unsubscribe and mark it as spam.
© 2023 Robert Elder Software Inc.
SocialSocialSocialSocialSocialSocialSocial
Privacy Policy      Store Policies      Terms of Use