Measures on the command line

In an earlier article I promised to demonstrate more 'magic words' for the command line. All you do is open a terminal, enter the magic word, hit Enter – and cool things happen! The magic word this time is units. The GNU Units program isn't installed by default in most Linux distributions, so you'll probably need to install it from your distribution's repository. Also, until you get to know GNU Units, I recommend that you enter units -v (v for verbose) on the command line. This makes the output a little more easy to understand.

The GNU Units program converts quantities from one unit system to another.

When you enter units -v you get the result shown in the first screenshot.

To do a conversion, just type the quantity and its units at the You have: prompt, and press Enter. Let's try 4139 feet (second screenshot).

At the You want: prompt, type the new units you want. We'll ask for centimeters, then press Enter (third screenshot).

The first line of the output shows the direct conversion: 4139 feet = 126156.72 centimeters. The second line shows the reciprocal of the conversion factor, then GNU Units prompts you for another conversion.

If you like, you can have a list of possible units you can convert to by pressing ?.

The cool factor

Do I hear you saying "So what? There are lots of online converters. Even Google does it."

Yes, but... Not only does GNU Units work offline, it has a simply enormous database of units it draws on. This document is stored in /usr/share/misc/units.dat and it's a fascinating read. Try converting cubic barleycorns to British firkins in any other program, or Montana miners inches to cubic meters per second. And did you know that 1 borgis is exactly 9 didotpoint? Or that spooned cocoa weighs 82 grams per US cup?

Even more useful is the way GNU Units automatically converts units during calculations. How many USA beer kegs of water fall on a block of land 55 meters wide by 191 feet, 8 inches long during a storm that dumps 1 Biblical cubit of rain? See the last screenshot.

To exit the program, just enter Ctrl + c or Ctrl + d. For more on GNU Units, see the GNU page and Wikipedia, or enter man units in a terminal after units has been installed. GNU Units was written in C by Adrian Mariano, a mathematician at Cornell University. A Java version is available on SourceForge.

License

Verbatim copying and distribution of this entire article are permitted worldwide, without royalty, in any medium, provided this notice is preserved.