Summary

Ever thought your terminal could be a source of daily wisdom or humor? It’s true! With the fortune command, you can receive a random quote, joke, or piece of advice every time you execute it. Whether you’re working or enjoying a break, a short fortune can lift your mood and encourage new ideas.

What Is the fortune Command?

The fortune command is a classic tool, with roots tracing back to the 1980s when it was bundled withUnix. This command’s primary purpose is to display a random quote, proverb, or humorous saying each time you run it. It is your digital fortune cookie that offers a slice of wisdom or a chuckle whenever you need it.

Why add fortunes to your terminal? It’s just for fun! You can use them in your system’s message of the day. Personally, I find the fortune command handy when I need some quick text to testregular expressionsin the terminal.

Running fortune command in Linux terminal.

To use fortune, you first need to make sure it’s already present in your system. Theoriginal version of fortunewas created for a Unix-like operating system called NetBSD. However, if you’re using Linux, you can’t use that original version directly. Instead, you have to use a modified version calledfortune-mod.

So, when you’re installing fortune on a Linux system, you’re actually installing the fortune-mod package. This version has been adapted specifically to work on Linux.

Adding fortunes text to a plain text file using nano text editor in Linux terminal.

On all major Linux systems, you can install fortune via default package manager. For example, on Ubuntu or Debian, run:

To get it on Fedora and other RPM-based systems, run:

Converting plain text file to fortune supported file using strfile command.

For Arch Linux, run this:

Once installed, all you need to do is type fortune into the terminal, hit enter, and wait for the magic to happen.

fortune Options

When you run a fortune without any arguments, it randomly selects an epigram from the fortune’s database. However, it also comes with several options that let you enhance your experience. Let’s check out some of them:

-a

Listing all fortune source files using the -f option of fortune command.

This option includes both offensive and inoffensive fortunes.

-s

Displaying random saying from your newly created fortune file.

This option tells fortune to only display short quotes. (less than 160 characters)

Serves up the long ones (more than 160 characters)

Piping fortune output to cowsay command to display random saying along with ASCII art.

-c

-f

Print a list of files that will be searched, without actually printing a fortune.

Tux displays random statements using fortune and cowsay commands.

-e

Using this option gives all files an equal shot. By default, longer files have a higher chance of being selected.

Making the ouptut of fortune and cowsay colorful using the lolcat command.

-i

Ignore the case when matching patterns with -m.

-m

Use this to search for fortunes that match a particular keyword. For example, if you want to see fortunes about love, you’re able to typefortune -m love.

Adding fortune command along with cowsay to the end of bashrc file.

-n

Set the maximum length for a fortune to be considered short. The default is 160 characters. Anything longer fortune will classify as “long.”

Random fortune appearing on a Linux terminal after being launched.

Furthermore, fortune allows you to choose from different categories, such as, if you’re a fan of literature or riddles, you can instruct fortune to only show quotes from those categories.

You can also combine multiple options together, such as if you want a short, offensive quote from the literature category, run this:

Create Your Own fortune File

What if I told you that you could create your own fortunes? That’s right, it’s a straightforward process. All you need is to create your custom fortune file and make it compatible with the fortune command’s requirements. In this way, you’re not bound to the predefined messages.

To try it out, simply open your terminal and create a plain text document using yourfavorite text editor.

Next, start adding your fortunes and make sure that each fortune is separated by a line with a percent sign % on it.

Once your file is ready, you need to convert it to a format that fortune can use. You can do this with the strfile command, which generates the necessary “funny.dat” file for the fortune command:

Now,move both files to the directorywhere fortune looks for its data. This location can vary, but it often looks like “/usr/share/games/fortunes/” or “/usr/local/share/games/fortunes/”.

You can verify the movement by running this command:

The percentage here gives you a rough idea of how much of the total database size is taken up by a particular file.

Finally, you can use the fortune command to get fortunes from your customs file.

Every time you run the command, one of your hand-crafted fortunes will appear.

Combine fortune and cowsay for Extra Fun

If you think the fortune command is cool, wait until you see it combined with cowsay. This fun command displays anASCII artcow in your terminal that says whatever you input. There’s no need for arguments—just provide some text, and you’re good to go.

You can get cowsay from your default package manager such as on Ubuntu or Debian, use apt:

Now, let’spipefortune output with cowasy to produce something very funny and interesting.

Check it out! You’ve got a cow dispensing wisdom right there in your terminal. Just what you always wanted!

But wait—there’s more! you may also change the character delivering your fortune. For example, use-ffollowed by the character’s name to pick any other ASCII creature:

you’re able to also make your tuxcolorful by piping the lolcat command.

View Fortune on Terminal Startup

Want to see a friendly dose of wisdom every time you open a bash terminal? If yes, then you need to add a fortune command to the end of your~/.bashrc file.

The first thing you need to do is to edit your shell configuration file. Forbash shell, the configuration file is likely ~/.bashrc, and forzsh, it’s ~/.zshrc. I’m using bash, so let’s open it with:

Next, scroll to the bottom of the file and add the following line:

This will ensure that every time you open your terminal, you’ll see a fortune delivered by a cow.

After saving changes, press Ctrl+X to exit the editor. To see the changes happen, you can eitherrestart your terminalor execute:

Now, every time you launch your terminal, you’ll be greeted with a delightful fortune.

If you want to use different ASCII art creatures, such as tux, just replace thecowsaycommand in the ~/.bashrc file with thecowsay -f tuxcommand.

Use Fortune for Testing

Beyond its entertainment or enlightening uses, the fortune command has a few hidden applications. For example, you can often use it to generate random text files that are very useful for testing scripts and various commands.

To view the content of the file, run:

If you want to append multiple fortunes to the same file, you can do so in a loop.

Another clever use for fortune isgenerating random numbers. By using thewc commandwith the-coption, you can count the characters in each fortune:

Create a Random Picker Game

You can also use fortune to randomize data for other purposes. Say you have a list of friends, coworkers, or contest participants—just put their names into a file and let fortune pick a winner. To accomplish this, you’ll need to make your own fortune file, as explained above.

There are severalfun Linux commandsthat can brighten your day, much like the classic fortune command. Some that I find particularly amusing is thefake Linux hacking commands.