Ever want to select a random file on the command line? Or just randomize anything? Well it’s possible using shuf.

We can use the shuf command to shuffle any input from stdin. Use

ls | shuf -n1

to pull a random file. The -n1 switch tells shuf to select only the first file. Without it you’ll get a totally randomized list of the entire directory. Neat!


shuf is a utility from GNU coreutils. It should be available out of the box on any regular linux install, but for OS X you will have to take an extra step.


On OS X, coreutils can be installed on the commandline using homebrew:

brew install coreutils

Important Note for OSX

Now that coreutils is installed you can call each coreutils application by its prefixed name, just add a g in front:

ls | gshuf -n1

Chain it

You can use the output of this command to send it to other applications. For example, on OS X we can use the open command to actually open the random file we’ve selected:

open $(ls | gshuf -n1)

blog comments powered by Disqus


20 April 2014