Sunday, 13 April 2014

Shuf - generate random permutations on Linux



Its always great to find a new Linux command that I have never heard of but is really useful. The command 'shuf' comes in to this category. It should be installed by default on most Linux systems as its part of the GNU Coreutils.

I wanted to generate random MAC address's from a text file which was simple with :

$ shuf -n 2 MACList | sort | uniq
00:A0:C8:14:C2:22
00:A0:C9:14:C8:29


Examples of using 'Shuf'

  • Generate 10 random numbers between 1 and 999

$ shuf -i 1-999 -n 10
182
588
696
841
574
204
173
641
958
289

  • Output results to a file
$ shuf -i 1-999 -n 10 -o results.txt
$ head -n 1 results.txt
749


  • Sort results and ensure numbers are unique

$ shuf -i 1-999 -n 10 | sort | uniq
101
142
298
396
508
564
793
870
923
95

  • Generate random words from Linux dictionary
$ shuf -n 2 /usr/share/dict/words
universe's
phosphorus


























No comments:

Post a Comment