units
Convert values between different units
TLDR
Run in interactive mode
List all units containing a specific string in interactive mode
Show the conversion between two simple units
Convert between units with quantities
Show the conversion between two compound units
Show the conversion between units with different dimensions
Show the conversion of byte multipliers
SYNOPSIS
units [options] [from-unit [to-unit]]
PARAMETERS
-v, --verbose
Displays the version number of the units database and the total number of units loaded upon startup.
-q, --quiet, --silent
Suppresses the display of the copyright message, the number of units loaded, and the prompts in interactive mode. Useful for scripting.
-t, --terse
Prints results in a terse format, suitable for scripts or when a compact output is desired. The input and output values are printed on a single line.
-s, --strict
Disallows conversions that are not strictly dimensional, such as temperature conversions between Fahrenheit and Celsius. By default, units can handle these non-linear conversions.
-f <file>, --file <file>
Loads units from the specified file instead of the default units database. This allows users to use custom or alternative unit definitions.
-c, --check
Checks the units database for inconsistencies, such as undefined units or dimensional errors. Useful for validating custom unit definitions.
-e <expression>, --expression <expression>
Evaluates a unit expression directly from the command line without entering interactive mode. For example: units -e '1 meter to feet'
.
-d [<string>], --defined [<string>]
Lists all units whose names contain the specified string. If string is omitted, all defined units are listed. This option is also aliased as -l or --list.
-h, --help
Displays a help message and exits.
-V, --version
Outputs version information about the units command and exits.
DESCRIPTION
The units command is a versatile and powerful utility designed for converting quantities expressed in various units. It can handle a vast array of conversions, from simple imperial-to-metric transformations (e.g., meters to feet) to complex scientific and engineering calculations involving compound units (e.g., joules to ergs, or miles per hour to meters per second). By default, units operates interactively, prompting the user for the unit they have and the unit they desire. It leverages an extensive database of predefined units, including standard SI units, imperial units, scientific constants, and even currency exchange rates (though these might require manual updates). Beyond basic conversions, units performs crucial dimensional analysis, ensuring that conversions are dimensionally consistent and alerting the user if the requested units are incompatible. Advanced users can extend its capabilities by defining custom units or aliases in personal configuration files, making it a highly adaptable tool for scientists, engineers, and anyone requiring precise unit conversions.
CAVEATS
While powerful, units has a few considerations:
Currency Rates: The currency exchange rates in the default database can quickly become outdated. For up-to-the-minute rates, manual updates to the units database file or external services are needed.
Temperature Conversions: Direct temperature conversions (e.g., Celsius to Fahrenheit) are non-linear. By default, units might convert temperature differences unless specific syntax or the -s (strict) flag is used.
User-Defined Units: Custom units defined by users are not inherently portable across different systems unless the custom definition files are also transferred.
UNIT DATABASE
The core of units is its extensive database of unit definitions. On most Linux systems, this database is located at /usr/share/units/definitions.units or a similar path. Users can also create or extend this database by defining their own units, aliases, and conversion factors in a personal file, typically named ~/.units. These files allow for highly customized conversion capabilities tailored to specific needs.
INTERACTIVE VS. NON-INTERACTIVE USE
By default, units runs interactively, prompting 'You have: ' and 'You want: ' for user input. This mode is excellent for quick, ad-hoc conversions. For scripting or single-shot conversions, units can be used non-interactively by providing the from-unit and to-unit directly as command-line arguments (e.g., units '1 gallon' 'liters'
), or by using the -e flag for expression evaluation.
COMPLEX EXPRESSIONS
units is not limited to simple conversions. It can evaluate complex expressions involving arithmetic operations, parentheses, and mixed units. For example, you can calculate units '5 miles/hour + 10 meters/second' 'km/hr'
to combine and convert quantities within a single command, making it a powerful scientific calculator.
HISTORY
The units command has a long history as a fundamental utility in Unix-like operating systems, tracing its roots back to early BSD Unix distributions. It was developed to provide a straightforward and robust way for users, particularly in scientific and engineering fields, to perform unit conversions without needing to manually remember conversion factors. The GNU units program, a widely adopted implementation, has further enhanced its capabilities with an expanded unit database, support for more complex expressions, and additional features for scripting, solidifying its place as an indispensable tool for accurate measurement conversions in the Linux environment.