8

How can I create a fortunes file and add it to the fortune* command on the shell?

*Fortune - "console command to print a random, hopefully interesting, adage."

3 Answers 3

13

To get you own custom fortune cookies first create a file and add the texts hat you want to be shown as a fortune.
In your text editor paste the text (string) you want to be show and between each string to be displayed there has to be a %

assuming we are creating a quotes fortune,with filename quotes

sudo nano quotes

paste in your text and edit like this

Limits exist only in your mind!
%
"Maybe this world is another planet’s hell."
— Aldous Huxley
%
"Choices are the hinges of destiny." —Edwin Markham
%
“Sometimes it’s the smallest decisions that can 
change your life forever." —Keri Russell
%
“We are the creative force of our life, and through our 
own decisions rather than our conditions, if we carefully
learn to do certain things, we can accomplish
those goals.” —Stephen Covey

save the file and create a dat file:

strfile -c % quotes quotes.dat

This creates a .dat file for your cookie file, which contains a header structure and a table of file offsets for each group of lines. This allows random access of the strings.

to see your fortune, do:

fortune quotes

To make your cookies appear as a standard fortune cookie when you simply type fortune, add quotes and quotes.dat to /usr/share/games/fortunes/
If you want to add or subtract any string from your custom fortune cookie file,you will need to to run the strfile -c % quotes quotes.dat to create a new dat file again

You can also add your fortunes to bash such that when you open the shell you are greated by your custom fortune.
do nano ~/.bashrc and add fortune /path/to/quote | lolcat to the end of the file and save

Now close your terminal and open a new terminal to see it work

1
  • 1
    Cool thanks for the guide. Is it a security risk to link from /usr/share/games/fortunes to files that are writable by normal users or is the fortune parsing safe? If not, I will modify permissions of my custom fortune files before and after updating them...
    – Nicolas
    Sep 29, 2018 at 16:57
7

This is explained in the manpages for fortune(6) and (especially) strfile(1).

Basically, you create a text file that contains your "fortune cookies" separated by a line that contains a single % sign and nothing else. Each fortune cookie can be 1 or more lines (including empty lines). A random example from one of the default fortune cookie files:

A day for firm decisions!!!!!  Or is it?
%
A few hours grace before the madness begins again.
%
A gift of a flower will soon be made to you.
%
A long-forgotten loved one will appear soon.

Buy the negatives at any price.
%
A tall, dark stranger will have more fun than you.

Then you use the strfile utility to create a binary search index (see its manpage for extra options). When you put both the text file and the binary index file in /usr/share/games/fortunes/, your new fortune cookies should be available.

1
  • strfile -c % f1.txt f1.dat ; then run fortune f1.dat ; received error: "No fortunes found" Nov 24, 2015 at 13:25
2

The fortune command is documented here: man fortune. The man page tells you how to specify your own fortunes file. The default files live in /usr/share/games/fortunes (I found them by typing locate fortune.

If you want to permanently add your custom file, there are several ways: put it in /usr/share/games/fortunes, make an alias (help alias) to fortune, etc.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .