The GNU sort text utility features a non-standard -R option to randomize input lines.

OSX sort does not have this extension. Is there similar functionality available in another text filter?

4 Answers 4


If you want, you can install GNU sort through GNU's coreutils package over Homebrew, which is a package manager for OS X.

Running this would install Homebrew.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then just follow the installation instructions. When Homebrew is installed, run

brew install coreutils

This will install GNU sort as gsort, so you can use it like sort on any GNU Linux.

Alternatively, have a look at these Stack Overflow questions, which mention a couple of methods:

How can I randomize the lines in a file using a standard tools on Redhat Linux
How can I shuffle the lines of a text file in Unix command line?

Or take a look at this commandlinefu.com page:

Randomize lines (opposite of | sort)

  • The homebrew option works, as would the linked awk or perl constructions (though I imagine they are slower.) The rest are no go, as they are GNU-only.
    – phs
    Sep 12, 2011 at 18:05

On OS X, if you don't want to install homebrew (but you really should), you could use perl or ruby:

perl -MList::Util -e 'print List::Util::shuffle <>'


ruby -e 'puts STDIN.readlines.shuffle'

Use shuf or sort from coreutils package, but then you've to add /usr/local/bin to your PATH in ~/.bashrc file, for example:

export PATH=/usr/local/sbin:/usr/local/bin:$PATH

Alternatively you can try ex:

ex -s +"%s/^/\=reltimestr(reltime())[-2:].' '" +"sort n" +"%s/^\S* //" +%p -cq! /dev/stdin

Source: How to shuffle a list in vim?


You could install the rl command via homebrew (brew install randomize-lines).

It is quite fast and has options to limit the number of returned items, as well as specify the delimiter (space instead of line feed, for instance).

(See also https://stackoverflow.com/a/42056195/43615.)

You must log in to answer this question.

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