The fortune command is generally considered one of the just-for-fun commands that you\u2019ll find on linux systems, but it can prove useful in some interesting ways.\nHow it works\nProbably most Linux users run the fortune command only when they\u2019re bored, though I\u2019ve known a few who added the fortune command to the end of their .bashrc files so that every login would provide a little quote or a saying that they\u2019d ponder for as much as 30 seconds before proceeding to more serious work.\nThe fortune command is, however, more versatile than many Linux users realize. In fact, most of the responses to typing \u201cfortune\u201d are not really fortunes at all. Rather than predicting your future or even just the outcome of your day, they provide quotes or lighthearted comments.\nBy default, the responses to typing \u201cfortune\u201d come from any of a fairly large series of files. On my fedora system, there are 48 such files that my \u201cfortune\u201d can be drawn from.\nWhat you can do with the fortune command\nIf you want your fortune to be derived from a file related to a particular subject area, you can provide it as an argument like this:\n$ fortune food\nA waist is a terrible thing to mind.\n -- Ziggy\n\nTo do this, however, it\u2019s good to know what subject areas are available. On my systems, these are my options:\n$ cd \/usr\/share\/games\/fortune; ls *.dat | sed "s\/.dat\/\/" | column\nart food law people sports\nascii-art fortunes linux perl startrek\nbofh-excuses goedel literature pets tao\ncomputers hitchhiker love platitudes translate-me\ncookie humorists magic politics wisdom\ndefinitions humorix-misc medicine pratchett work\ndisclaimer humorix-stories miscellaneous riddles zippy\ndrugs kernelnewbies news rules-of-acquisition\neducation kids osfortune science\nethnic knghtbrd paradoxum songs-poems\n\nOnce you know the topic areas, you can use any of them to get a fortune which aligns with your mood.\n$ fortune politics\nThe time for action is past! Now is the time for senseless bickering.\n\nCreating your own fortunes\nIf the topic areas don\u2019t meet your needs, don\u2019t fret. You can make your own! In fact, it\u2019s quite easy to do so. You need to create a file with a particular format and then run one command to make the data compatible with the fortune command\u2019s requirements.\nTo test this out, I created a file with a few happy quotes. I need a daily dose of happiness and thought these quotes would be nice to work with. I referred to my file as \u201chappy quotes\u201d and added this content:\n$ cat happy_quotes\nYou're never fully dressed without a smile.\n%\nLet a smile be your umbrella.\n%\nHappiness depends upon ourselves.\n%\nHappiness is when what you think, what you say, and what you do are in harmony.\n\nNotice that I used % signs to separate the lines of text. None are needed at the bottom.\nThe next thing I did was run the command below to create the quotes.dat file that the fortune command requires.\n$ strfile -c % happy_quotes happy_quotes.dat\n\nWhen I run a command like fortune happy_quotes, I get a response like this:\n$ fortune happy_quotes\nYou're never fully dressed without a smile.\n\nOf course, with only four happy quotes, I\u2019m going to quickly get tired of the responses. And I\u2019m afraid that no one else who uses my system will see any of my happy quotes because the file is in my home directory, not the \/usr\/share\/games\/fortune directory, though I could change that.\nOther uses for \u201cfortunes\u201d\nIn addition to its entertainment or enlightenment value, the fortune command offers a few more uses that might not be immediately obvious. I use the fortune command to create random text files that I use for testing various commands and scripts. A command like this generates a small file of that variety:\n$ fortune > file1\n$ ls -l file1\n-rw-r--r--. 1 shs shs 244 Sep 22 13:53 file1\n\nIf you need a larger randomly generated text file, just run the command just shown and then follow it with additional commands of this variety:\n$ fortune >> file1\n$ fortune >> file1\n\nThe file should soon be large enough.\nYou can also use the responses from the fortune command to generate random numbers. The -c argument to the wc command yields the count of characters in each fortune.\n$ fortune | wc -c\n107\n$ fortune | wc -c\n226\n$ fortune | wc -c\n86\n\nIf the resultant numbers are not sufficiently large or sufficiently random for your purpose, create an alias with as many calls to fortune as you might need.\n$ alias randnum='expr `fortune | wc -c` * `fortune | wc -c` * `fortune | wc -c`'\n$ randnum\n1793660\n$ randnum\n878292\n\nOf course, you\u2019re not going to get any small numbers by multiplying the number of characters in each of three text files.\nAnother option is to use the fortune command to randomize data for some other use. For example, put all your friends\u2019, coworkers\u2019 or contest participants\u2019 names into a file and use the fortune command to pick a winner.\nOnce you\u2019ve got the names in a text file with lines separated by % characters, you can create your.dat file and do a drawing that depends on the randomness of the fortune command to play fair.\n$ strfile -c % friends friends.dat\n"friends.dat" created\nThere were 15 strings\nLongest string: 12 bytes\nShortest string: 4 bytes\n\n$ fortune friends\nRebecca\n\nHow many fortunes are available?\nOut of curiosity, I wrote a script to count the number of fortunes available in the \/usr\/share\/games\/fortune directory on my system. The script looks like this:\n#!\/bin\/bash\n\ntotal=0\n\nfor file in `ls \/usr\/share\/games\/fortune | grep -v "."`\ndo\n num=`grep % $file | wc -l`\n num=`expr $num - 1`\n total=`expr $total + $num`\ndone\n\nThe grep command ensures that only the fortune source files (those without file extensions) are used to collect the tally. I was pleased by the number available as it makes it unlikely that you\u2019ll see any particular fortune more than once.\n$ count_all_fortunes\n16139\n\nWrap-up\nThe fortune command can be mildly entertaining or quite useful. Since the fortune I pulled out of my fortune cookie at lunch time provided this fortune, I\u2019m going to assume that at least a few of you enjoyed this post. ;-)\nThey\u2019ll definitely remember all your efforts.