(Potentially) funny random filenames for Bash scripts

It’s been a busy week, and I’ll be writing up my WWDC thoughts and a couple of TUAW posts after a plane ride today. In the meantime, I made you a thing.

This quick function is part of a script I wrote to generate multiple files filled with random Markdown lipsum with an index for Marked testing.

Numbered files are boring, and truly random character sequences aren’t much fun. Aspell has a master dictionary that happens to contain a lot of potentially funny words. I decided to dump those and randomly pick two to string together as filenames.

The function uses a dictionary file (single word per line) and shuf, available through Homebrew in the Gnu coreutils package (brew install coreutils). You can create a dictionary file using Aspell (brew install aspell) with the command aspell dump master > dictionary.txt (or just grab the one I made). Be sure to edit the “wordfile” variable in the function to point to the location of your dictionary.

gen_random_filename.bashraw
# Bash function gen_random_filename
# Description: Generates random two-word names
# Requires a dictionary file, easily generated with `aspell dump master > dictionary.txt`
# or grab it from https://gist.githubusercontent.com/ttscoff/55493fe89c35ec1588ba/raw/
# Requires shuf (brew install coreutils)
#
# Example results:
# darkest_pickpockets
# besets_struts
# unzip_Malone

gen_random_filename() {
	local wordfile=~/words/dictionary.txt
	local randwords
	local title

	if [[ $(which shuf) ]]; then
		randwords=`shuf -n 2 $wordfile|sed 's/[^a-zA-Z]//g'`
		title=`echo $randwords|tr ' ' '_'`
	else
		cat <<-EOS
		$FUNCNAME requires the utility shuf (or gshuf) to generate random names.
		Use homebrew to install "coreutils," which includes shuf.
		EOS
		return
	fi
	echo $title
}

For the try-before-you-buy fans, here are sample results using the Aspell dictionary:

$ while true; do gen_random_filename; done
worldviews_punctualitys
eddys_dodge
Beaujolais_unjustifiably
doxology_teatimes
peignoirs_toilet
paling_landlords
OToole_Superglue
verrucae_resented
casseroling_rumble
Tharps_interbred
misjudgment_strum
workhouse_frisson
solstices_fifes
isotherms_interlocks
riotously_Scud
knockabout_skinflints
volcano_resewn
nubs_imprudences
geezers_disturbing
^C

Ryan Irelan has produced a series of shell trick videos based on BrettTerpstra.com posts. Readers can get 10% off using the coupon code TERPSTRA.

Brett Terpstra

Brett is a writer and developer living in Minnesota, USA. You can follow him as ttscoff on Twitter, GitHub, and Mastodon. Keep up with this blog by subscribing in your favorite news reader.

This content is supported by readers like you.