Sunday, 21 February 2016

Obtain Random Words from a Dictionary in Linux

It can be very useful to be able to quickly generate random words from a dictionary in Linux. One common use case I have is generating random responses for the Security Questions on web sites. This is to avoid anyone easily guessing what the answers are. Typically the questions tend to be the same and not very difficult to guess. I save the actual responses in the Notes section of the Password Manager than I use.

Obtain A Dictionary Text File

There is a great dictionary list that is available with GNU Aspell (ftp://ftp.gnu.org/gnu/aspell/dict/0index.html) which provides a collection of International ‘words’ files for /usr/share/dict.
To install on Arch Linux :
$ pacman -S words
$ wc -l /usr/share/dict/usa
119095 /usr/share/dict/usa
There are a number of dictionaries installed under /usr/share/dict/ that you can choose from
$ ls /usr/share/dict/
american-english british british-english catala catalan finnish french german italian ngerman ogerman spanish usa

shuf - generate random permutations

We can use the shuf command to generate random words using a dictionary. Install shuf if its not installed :
$ pacman -S shuf
Then use the n argument to define the number of words you would like e.g.
$ shuf -n5 /usr/share/dict/usa
macroeconomic
visualizes
shabbiest
sen
chortler
To easily access this I have an alias :
alias GenerateRandomWord=’shuf -n5 /usr/share/dict/usa’

No comments:

Post a Comment