Fortune Files in Linux

Fortune is a small Linux program with a simple purpose: display a random string of text to the user. The fortune utility draws its quotes, pearls of wisdom, jokes, and factoids from a set of standard and custom databases, and these databases are very easy to create and manage. This post will explain how to set up, customize, and use fortune on your machine.

Installing Fortune

Open a terminal window and enter the following command to install fortune on a Debian/Ubuntu box:

sudo apt-get install fortune

Running Fortune

Once fortune is installed, it can be invoked by running the ‘fortune’ command. This should cause an adage or epigram to be printed to the screen. By default, three fortune databases are installed with the application: “fortunes,” “literature,” and “riddles.” The fortune program accepts database names as optional parameters. For instance, ‘fortune literature’ will return a random literature quotation, while ‘fortune literature riddles’ would produce a quotation or a riddle. If no particular database is specified, fortune draws strings from across all of the available databases.

Custom Fortune Databases

It is extremely easy to create and install custom fortune databases. First, build a file of fortune strings, separated by lines containing only the ‘%’ character. To practice, create a file named ‘test’ and give it the following contents:

test1
%
test2
%
test3
%

Next, use the strfile utility to convert this file into a database. This will produce a new *.dat file in binary format. Using the ‘test’ file, the following would create ‘test.dat’:

strfile test

Finally, move the files to the fortune database directory. On Ubuntu, this is /usr/share/games/fortunes.

sudo cp test* /usr/share/games/fortunes

After following this example, ‘fortune test’ should display one of the three test messages we defined.

Fortunes at Terminal Login

It’s fun to see a fortune displayed whenever you open a new terminal window. To set this up, just add a single line to the end of your ~/.bashrc file:

fortune

Simple, right? Remember, this command could be followed by a list of database names for more fine-grain control.

Wow. It's Quiet Here...

Be the first to start the conversation!


Leave a Reply:

Gravatar Image

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>