---
title: Even better random filenames
date: 2014-06-09 10:51
slug: even-better-random-filenames
categories:
- Code
tags:
- lipsum
- scripting
- terminal
---

This is an evolution of the [random filename generator][brettterpstra] I posted last week. Based on [comments from BrianEnigma][brettterpstra 2], 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][ckyp], or download the "Database files only" from [WordNet][dict] 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.

Examples:

	$ gen_random_filename
	measly_hypostatization

	$ gen_random_filename a b
	arrhythmic_bering_time

	$ gen_random_filename z
	zambian_zoysia_matrella

And the script:



```shell
# 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
	counter=1
	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]
		fi
		shift
	done

	# 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
	else
		cat <<-EOS
		$FUNCNAME requires the utility shuf to generate random names.
		Use homebrew to install "coreutils," which includes shuf.
		EOS
		return
	fi
	echo $title
}
```

[View the code on GitHub](https://gist.github.com/25ab96138e0a5e4537ea)



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

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

mkdir include 2> /dev/null

includefile="include/$title"
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
```

[brettterpstra]: http://brettterpstra.com/2014/06/07/potentially-funny-random-filenames-for-bash-scripts/
[brettterpstra 2]: http://brettterpstra.com/2014/06/07/potentially-funny-random-filenames-for-bash-scripts/#comment-1424119025
[brettterpstra 3]: http://brettterpstra.com/2013/10/26/textexpander-and-lorem-ingsoc-in-wonderland/
[brettterpstra 4]: http://brettterpstra.com/2014/01/16/a-service-for-creating-marked-indexes/
[ckyp]: http://ckyp.us/yejw
[dict]: http://wordnet.princeton.edu/wordnet/download/current-version/
[marked2app]: http://marked2app.com

---

Brett is a writer and developer living in Minnesota, USA. You can follow him as ttscoff. This content is supported by readers like you. [More about Brett](https://brettterpstra.com/contact.md), including social links.

Copyright © 2026 Brett Terpstra. All rights reserved.
