The fortune command might be more versatile than you realize. Credit: Yeshi Kangrang The fortune command is generally considered one of the just-for-fun commands that you’ll find on linux systems, but it can prove useful in some interesting ways. How it works Probably most Linux users run the fortune command only when they’re bored, though I’ve 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’d ponder for as much as 30 seconds before proceeding to more serious work. The fortune command is, however, more versatile than many Linux users realize. In fact, most of the responses to typing “fortune” 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. By default, the responses to typing “fortune” come from any of a fairly large series of files. On my fedora system, there are 48 such files that my “fortune” can be drawn from. What you can do with the fortune command If 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: $ fortune food A waist is a terrible thing to mind. -- Ziggy To do this, however, it’s good to know what subject areas are available. On my systems, these are my options: $ cd /usr/share/games/fortune; ls *.dat | sed "s/.dat//" | column art food law people sports ascii-art fortunes linux perl startrek bofh-excuses goedel literature pets tao computers hitchhiker love platitudes translate-me cookie humorists magic politics wisdom definitions humorix-misc medicine pratchett work disclaimer humorix-stories miscellaneous riddles zippy drugs kernelnewbies news rules-of-acquisition education kids osfortune science ethnic knghtbrd paradoxum songs-poems Once you know the topic areas, you can use any of them to get a fortune which aligns with your mood. $ fortune politics The time for action is past! Now is the time for senseless bickering. Creating your own fortunes If the topic areas don’t meet your needs, don’t fret. You can make your own! In fact, it’s 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’s requirements. To 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 “happy quotes” and added this content: $ cat happy_quotes You're never fully dressed without a smile. % Let a smile be your umbrella. % Happiness depends upon ourselves. % Happiness is when what you think, what you say, and what you do are in harmony. Notice that I used % signs to separate the lines of text. None are needed at the bottom. The next thing I did was run the command below to create the quotes.dat file that the fortune command requires. $ strfile -c % happy_quotes happy_quotes.dat When I run a command like fortune happy_quotes, I get a response like this: $ fortune happy_quotes You're never fully dressed without a smile. Of course, with only four happy quotes, I’m going to quickly get tired of the responses. And I’m 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. Other uses for “fortunes” In 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: $ fortune > file1 $ ls -l file1 -rw-r--r--. 1 shs shs 244 Sep 22 13:53 file1 If you need a larger randomly generated text file, just run the command just shown and then follow it with additional commands of this variety: $ fortune >> file1 $ fortune >> file1 The file should soon be large enough. You 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. $ fortune | wc -c 107 $ fortune | wc -c 226 $ fortune | wc -c 86 If 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. $ alias randnum='expr `fortune | wc -c` * `fortune | wc -c` * `fortune | wc -c`' $ randnum 1793660 $ randnum 878292 Of course, you’re not going to get any small numbers by multiplying the number of characters in each of three text files. Another option is to use the fortune command to randomize data for some other use. For example, put all your friends’, coworkers’ or contest participants’ names into a file and use the fortune command to pick a winner. Once you’ve 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. $ strfile -c % friends friends.dat "friends.dat" created There were 15 strings Longest string: 12 bytes Shortest string: 4 bytes $ fortune friends Rebecca How many fortunes are available? Out 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: #!/bin/bash total=0 for file in `ls /usr/share/games/fortune | grep -v "."` do num=`grep % $file | wc -l` num=`expr $num - 1` total=`expr $total + $num` done The 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’ll see any particular fortune more than once. $ count_all_fortunes 16139 Wrap-up The 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’m going to assume that at least a few of you enjoyed this post. 😉 They’ll definitely remember all your efforts. Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe