Even better random filenames

This is an evolution of the random filename generator I posted last week. Based on comments from BrianEnigma, this script allows you to specify “starts with” arguments to get an adjective and a noun using the WordNet dictionaries.

You can use the ready-made set I have here, or download the “Database files only” from WordNet and run this on the index.adj and index.noun files to create your libraries:

awk '{print $1}' index.adj > adjectives.txt

Edit the script below to point to the location of your dictionary files. Then just install and source it to start using. If you provide a single alphabet character as an argument, both the adjective and the noun will start with that letter. Provide two, separated by a space, and the adjective will start with the first one, the noun with the second. You can still provide no arguments to get random words from the full alphabet.


$ gen_random_filename

$ gen_random_filename a b

$ gen_random_filename z

And the script:

# Bash function gen_random_filename
# Description: Generates random file names
# Requires shuf (brew install coreutils)
# Requires a list of adjectives and nouns (1 per line)

gen_random_filename() {
	local adjs=~/words/adjectives.txt
	local nouns=~/words/nouns.txt
	local adj noun title starts_with_1 starts_with_2 counter

	# process arguments for "starts_with" variables
	while [[ -n $1 ]]; do
		# only accept single letter (a-z) arguments
		if [[ $1 =~ ^[a-zA-Z]$ ]]; then
			eval starts_with_$counter="$1"
			counter=$[counter + 1]

	# if starts_with_1 has a value and starts_with_2 doesn't, duplicate
	[[ -z $starts_with_2 && -n $starts_with_1 ]] && starts_with_2=$starts_with_1

	# if shuf is installed...
	if [[ $(which shuf) ]]; then
		# grep for starts_with value and pick random result
		# if starts_with is empty, grep will return all entries
		adj=`grep --color=never -e "^$starts_with_1" $adjs | shuf -n 1`
		noun=`grep --color=never -e "^$starts_with_2" $nouns | shuf -n 1`
		# concatenate adjective and noun
		title=`echo ${adj}_${noun}`
	# otherwise, error
		cat <<-EOS
		$FUNCNAME requires the utility shuf to generate random names.
		Use homebrew to install "coreutils," which includes shuf.
	echo $title

I’m using this with a script that generates large blocks of random lorem ipsum (based on the Lorem Ingsoc scripts) in a series of files with a Marked index file for testing Marked 2. Here’s the basic idea, just for reference. It’s truncated, but shows how to use the function above in a script:

if [[ -z $title ]]; then
	echo "Generating random title"
	title=$(gen_random_filename $start_with_1 $start_with_2)

mkdir include 2> /dev/null

echo -e "# Test Index\n\n" > $title-index.marked
jot -w "${includefile}-%d.md" - 1 $count|xargs -I % touch "%"
jot -w "${includefile}-%d.md" - 1 $count|xargs -I % echo -e "<<[%]\n" >> ${title}-index.marked
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.