Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to get random words from the dictionary databases like from the dict-wn or freedict's packages? I'm looking for a way to do that by either using a bash script or try a command tool if available.

share|improve this question
up vote 7 down vote accepted

If the wordlist is a plain text file with one word per line, then one option is the shuf command e.g.

$ shuf -n5 /usr/share/dict/american-english

See man shuf

SHUF(1)                          User Commands                         SHUF(1)

       shuf - generate random permutations

       shuf [OPTION]... [FILE]
       shuf -e [OPTION]... [ARG]...
       shuf -i LO-HI [OPTION]...

       Write a random permutation of the input lines to standard output.
share|improve this answer

I'd favour shuf for plucking lines from a file. We also need to be careful with the words because it's a tab-delimited file, not just whitespace.

$ shuf -n5 /usr/share/dictd/wn.index | cut -d$'\t' -f1
table game
tetraneuris grandiflora
decimal digit

shuf takes 0.011s here. sort -R (even without the awk) takes 2.427s. This is because shuf is just seeking through the file n times while sort is reading and processing every line... In this case that's 147311 lines.

share|improve this answer

There are plenty of ways to get a random word. Example to retrieve five words:

$ sort -R /usr/share/dictd/freedict-nld-eng.index | awk 'NR <= 5 { print $1 }'

See man sort for the meaning of the -R option (hint: Random). The awk expression filters on the first five records (lines) and prints the first field (not the other columns).

share|improve this answer
@rusty No problem, sort is not the most efficient program for this purpose either, it creates temporary files for large files. A better program (in terms of memory) would first scan for the number of lines, then pick some random numbers and then scan again for those lines. – Lekensteyn Jul 7 '14 at 17:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.