Imagine you have a folder with lots of notes that you keep around for inspiration. Perhaps you want to open one of them at random to serve as inspiration. How would you do it? You could always put on a blindfold, spin your chair around several times and then attempt to click on a file in your nauseated state as you try your best not to not fall off the chair.
Well now there's an easier and more handy way to open up a random file, and there's no need to get dizzy and risk smashing your computer.
The GNU/Linux command line, in all its potency and glory, presents us with a near-effortless way to open up a random file. It combines the powerful GNU find and GNU's shuf.
Here's an example:
find /somedirectory/thingstoshuffle/ -type f -name '*.txt' -o -name '*.html' | shuf -n1
In plain English, the above translates as:
Find any regular file in this directory with the extension .txt or .html and then pass the output to shuf to pick one line at random.
The above command will provide you with a single filename. Now imagine that you want to do something more interesting with this command. Perhaps you simply want to open that file up in your favourite text editor or browser. For that, we create a Bash script with the following:
FILE="$(find FILE="$(find /somedirectory/thingstoshuffle/ -type f -name '*.txt' -o -name '*.html' | shuf -n1)" vim "$FILE"
The $FILE variable here is the randomly selected file. Executing the above script simply opens up the randomly generated file in Vim, but you can do anything that you want with it.
I should also mention that this command digs through all subfolders within that directory. If you want to avoid searching subfolders then you have to issue find's "-maxdepth 1" option.
There you go -- an easy way way to open up a random file, and all done without the need for some ad-ware or virus infested software/bloatware. Just one of the many wonders waiting to be discovered on the Linux command line.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.