Ubuntu Linux has a great tool called shuf, which works like head except that it gives you ten random lines. I couldn't find it on Homebrew. What is the simplest way of installing it on OS X?

  • 2
    It’s part of the gnu text utilities package. Seems to be part of coreutils, but I don’t know how stuff is packaged on homebrew.
    – jl8e
    Commented Aug 22, 2014 at 16:25

5 Answers 5


You can install coreutils with brew install coreutils.

shuf will be linked as gshuf. Read the caveats when you install coreutils.

  • 2
    The caveat just seems to be that you shouldn't use the tools' proper names, but prefix them with g (like "gshuf"), right? Commented Aug 22, 2014 at 16:35
  • 2
    Yep, or optionally configure your PATH so you get what you expect. Commented Aug 22, 2014 at 16:39
  • 3
    brew install coreutils installs the utils with the prefix g by default on osx, so you can install it using the above command.
    – Brad Parks
    Commented Jun 17, 2015 at 13:37
  • 1
    Mac OS X Catalina 10.15+, with bash enabled as the shell, does install the shuf command along with gshuf.
    – rjurney
    Commented Jan 23, 2020 at 19:20

Yet another solution is to learn about the tools supplied by the vendor. Certainly you could chain jot, paste, sort, cut, head and get the same results.

jot -r "$(wc -l FILE)" 1 |
paste - FILE |
sort -n |
cut -f 2- |
head -n 10
  • jot produces a random number from 1 to the number of lines in FILE for each line
  • paste pastes the random number to each line in FILE
  • sort sorts numeric each line
  • cut removes the random number from each line
  • head outputs the first 10 lines
  • 2
    I really like the essence of this answer, you never know when you might want to do the same thing on another machine where you aren't able to install extra tools for whatever reason
    – forquare
    Commented Aug 7, 2015 at 21:02
  • 1
    I had never heard about jot before. Thanks for expanding my knowledge. Commented Feb 7, 2018 at 11:16
  • While this answer kind of works, by default jot doesn't generate that much randomness, in the current version it seems like it only generates integers from 1 to 100, so while this will give you a different ordering of lines, it will be far from the uniform ordering you'd get from shuf
    – Erik
    Commented Sep 20, 2020 at 14:41

You can install coreutils with Macports as

sudo port install coreutils

This will put GNU core utils in /opt/local/bin with a g prepended

e.g. gshuf

More details on the package coreutils.

  • 2
    I used this one with macports, thanks. I don't like Homebrew much
    – Zorgatone
    Commented Aug 7, 2015 at 10:22
  • Great answer if you have sudo rights. I don't. Commented Feb 7, 2018 at 12:48
  • @tommy.carstensen Then ask your admin - they will have made it difficult for you to do this for a reason
    – mmmmmm
    Commented Feb 7, 2018 at 14:33

You can use sort -R

$ seq 5 | sort -R
  • 3
    Good answer, and works for both BSD and GNU sort.
    – msbit
    Commented Nov 12, 2021 at 3:38

Another option is to install randomize-lines(homebrew) package, which has an rl command which has similar functionality to shuf.

Usage: rl [OPTION]... [FILE]...
Randomize the lines of a file (or stdin).

  -c, --count=N  select N lines from the file
  -r, --reselect lines may be selected multiple times
  -o, --output=FILE
                 send output to file
  -d, --delimiter=DELIM
                 specify line delimiter (one character)
  -0, --null     set line delimiter to null character
                 (useful with find -print0)
  -n, --line-number
                 print line number with output lines
  -q, --quiet, --silent
                 do not output any errors or warnings
  -h, --help     display this help and exit
  -V, --version  output version information and exit

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .