Linux units Command
units is a powerful unit conversion tool in Linux. It can handle conversions between various physical quantities, including length, weight, temperature, speed, time, and more. This command-line tool is especially suited for engineers, scientists, and technicians who need to perform complex unit conversions.
Features of the units command:
- Supports conversions among more than 3000 units
- Can perform calculations with compound units (such as converting watt-hours to joules)
- Allows users to define their own units
- Provides both interactive and command-line modes of use
Installing the units command
Most Linux distributions do not install units by default; you need to install it manually:
Example
Debian/Ubuntu systems
sudo
apt-get install
units
CentOS/RHEL systems
sudo
yum install
units
Fedora systems
sudo
dnf
install
units
Arch Linux systems
sudo
pacman
-S
units
After installation, you can verify it was successful with the following command:
units --version
Basic Usage
The units command has two main modes of use: interactive and non-interactive.
Interactive mode
Simply enter units the command to enter interactive mode:
Example
$ units
2861
units,
109
prefixes,
109
nonlinear units
You have:
At the prompt, enter the units you want to convert, for example:
You have: 1 mile
You want: km
* 1.609344
/ 0.62137119
In the output:
*the following number is the multiplication factor (1 mile = 1.609344 kilometers)/the following number is the reciprocal factor (1 kilometer = 0.62137119 miles)
Non-interactive mode
You can perform conversions directly on the command line:
units "1 mile" "km"
The output is the same as in interactive mode.
Common unit conversion examples
Length conversions
Example
inches to centimeters
$ units
"12 inch"
"cm"
30.48
/
0.032808399
feet to meters
$ units
"6 feet"
"meters"
1.8288
/
0.54680665
Temperature conversions
Example
Fahrenheit to Celsius
$ units
"98.6 degF"
"degC"
37
/
0.027027027
Kelvin to Celsius
$ units
"300 K"
"degC"
26.85
/
0.037243908
Speed conversions
Example
miles per hour to kilometers per hour
$ units
"60 mph"
"kph"
96.56064
/
0.010356187
knots to meters per second
$ units
"15 knots"
"m/s"
7.7166667
/
0.12958963
Data storage conversions
Example
gigabytes to megabytes
$ units
"1 gibibyte"
"megabyte"
1073.7418
/
0.00093132257
Note: units uses binary prefixes by default (base 1024)
To use decimal prefixes (base 1000), use 'megabytes' instead of 'megabyte'
Advanced features
Compound unit conversions
units can handle complex compound units:
Example
Convert kilowatt-hours to joules
$ units
"1 kWh"
"joules"
3600000
/
2.7777778e-07
Convert pounds per square inch to pascals
$ units
"1 psi"
"pascal"
6894.7573
/
0.00014503774
Custom units
can be done during ~/.units define your own units in the file. For example, add a "football field" unit:
footballfield 100 m
Then you can use:
Example
$ units
"1 footballfield"
"km"
0.1
/
10
Currency conversion (requires an internet connection)
units supports currency conversion, but requires an internet connection to fetch the latest exchange rates:
$ units "100 USD" "EUR"
Note: Currency conversion may not be available on some systems or may require additional configuration.
Practical tips
-
View the list of available units: enter in interactive mode
?orhelpto view help information -
Fuzzy search: after typing part of a unit name, press Tab to auto-complete
-
History records: in interactive mode, use the up and down arrow keys to browse command history
-
Precise control of output: When using
-vthe option displays more detailed information
units -v "1 lightyear" "km"
- Check unit compatibility: When using
-cthe option only checks whether units are compatible without performing a conversion
units -c "joules" "watt hours"
Frequently Asked Questions
Why are some unit conversion results inaccurate?
units uses a predefined unit database; some conversions (especially currency) may have limited precision or use approximate values. For precise scientific calculations, consult specialized reference materials.
How do I update the unit database?
The unit database is usually located at /usr/share/units/definitions.units. You can edit this file manually or wait for a system update.
Can I perform batch conversions?
You can write a shell script combined with the units command to perform batch conversions:
Example
#!/bin/bash
for
value
in
1
2
5
10
;
do
result
=$
(
units
"$value feet"
"meters"
|
awk
'NR==1 {print $2}'
)
echo
"$value feet = $result meters"
done
Summary
The units command is a powerful and flexible unit conversion tool in Linux, suitable for a wide range of scientific, engineering, and everyday computing scenarios. Through this article, you should have mastered:
- the basic installation and use of the units command
- unit conversion techniques for common physical quantities
- advanced features such as compound unit conversions and custom units
- practical tips and problem-solving methods for real-world applications
To learn more about the features of the units command, consult its manual page:
man units
Now, try using the units command to solve your next unit conversion problem!
Linux Command Reference