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."
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
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
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:
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.
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
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.
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
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.