Robert Elder Software Inc.
  • Home
  • Store
  • Blog
  • Contact
  • Home
  • Store
  • Blog
  • Contact
  • #linux
  • |
  • #commandline
  • |
  • #softwareengineering
  • |
  • #embeddedsystems
  • |
  • #compilers
  • ...
  • 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.

Intro To 'stty' Command In Linux
Intro To 'stty' Command In Linux
Published 2023-10-04
Terminal Block Mining Simulation Game
$1.00 CAD
Terminal Block Mining Simulation Game
Intro To 'nproc' Command In Linux
Intro To 'nproc' Command In Linux
Published 2023-07-15
Intro To 'comm' Command In Linux
Intro To 'comm' Command In Linux
Published 2023-09-06
How To Force The 'true' Command To Return 'false'
How To Force The 'true' Command To Return 'false'
Published 2023-07-09
A Surprisingly Common Mistake Involving Wildcards & The Find Command
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21
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
Intro To 'chroot' Command In Linux
Intro To 'chroot' Command In Linux
Published 2023-06-23
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.
© 2025 Robert Elder Software Inc.
SocialSocialSocialSocialSocialSocialSocial
Privacy Policy      Store Policies      Terms of Use