How to Convert Measurement Scales and Units in Linux Terminal Using units

Whether calculating bandwidth conversions, translating imperial dimensions into metric equivalents, or computing complex physical equations in science and engineering workflows, developers frequently need rapid, accurate unit conversions. Rather than opening a browser tab or writing ad-hoc conversion scripts, Linux provides an authoritative, native command-line engine: GNU units.

The units utility converts quantities expressed across thousands of measurement systems, covering everything from everyday distances and weights to composite physical dimensions (such as thermal conductivity, torque, and fuel economy) and binary data storage scales.


Installing the units Package on Linux

While some distributions include units by default, it is readily available across all major distribution package managers.

Ubuntu, Debian, and Linux Mint

sudo apt update
sudo apt install -y units

Fedora, CentOS, and AlmaLinux

sudo dnf install -y units

Arch Linux

sudo pacman -S units

Interactive Conversion Mode

Running units without arguments launches its interactive REPL prompt, which sequentially requests your starting value and your target unit:

units

An interactive exchange appears as follows:

You have: 100 miles / hour
You want: km / hour
        * 160.9344
        / 0.0062137119

The output reports two reciprocal figures:

  • First line (*): The conversion factor by which you multiply your source value to reach the target unit (100 mph = 160.9344 km/h).
  • Second line (/): The inverse divisor required to convert backward from the target unit to the source.

To exit interactive mode, press Ctrl + C or Ctrl + D.


Running Non-Interactive Conversions in Shell Scripts

For shell scripts, automated cron jobs, or rapid one-liners, pass your conversion parameters directly as command-line arguments using the --terse (-t) flag. The -t flag suppresses informational headers and prints strictly the numeric output.

1. Converting Imperial to Metric Distances

units -t "5 miles" "kilometres"

Outputs: 8.04672

2. Calculating Binary vs Decimal Data Storage

Storage drive manufacturers define gigabytes in decimal (10^9 bytes), whereas operating systems historically report gibibytes in binary (2^30 bytes). You can evaluate disk discrepancies instantly:

units -t "500 GB" "GiB"

Outputs: 465.66129

3. Converting Network Bandwidth Rates

To calculate how many megabytes per second a 250 Megabit-per-second internet line delivers:

units -t "250 megabit/sec" "megabyte/sec"

Outputs: 31.25


Handling Temperatures and Non-Linear Conversions

Unlike distances or weights where conversions are purely multiplicative, temperature scales (such as Celsius, Fahrenheit, and Kelvin) require affine offsets (e.g. adding 32 degrees or subtracting 273.15). In units, function-based temperature units begin with temp:

Conversion Command Syntax Calculated Output
Fahrenheit to Celsius units -t "tempF(72)" "tempC" 22.222222
Celsius to Fahrenheit units -t "tempC(100)" "tempF" 212
Celsius to Kelvin units -t "tempC(25)" "tempK" 298.15

Evaluating Complex Composite Dimensions

The strength of units lies in its dimensional analysis engine. If you enter incompatible physical dimensions—such as attempting to convert hours into kilograms—the utility detects the dimensional mismatch and warns you:

$ units "10 hours" "kg"
conformability error
        36000 s
        1 kg

Conversely, it effortlessly resolves complex multi-term dimensions such as automotive fuel efficiency:

units -t "35 miles / gallon" "litres / 100 km"

Because the unit definitions file (typically located at /usr/share/units/definitions.units) is open and extensible, you can even add custom domain-specific conversion constants to your local environment.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.