Linux units Command

Linux 命令大全Linux Command Reference


The units command is a powerful unit conversion tool in Linux systems. It can handle conversions between various physical quantities, including length, weight, temperature, speed, time, and more. This command-line tool is especially suitable for engineers, scientists, and technical personnel who need to perform complex unit conversions.

Features of the units command:

  • Supports conversion of more than 3,000 units
  • Can perform compound unit calculations (e.g., converting watt-hours to joules)
  • Allows users to define custom units
  • Provides both interactive and command-line usage modes

Installing the units Command

Most Linux distributions do not install units by default; you need to install it manually:

Examples

# 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 whether it was installed successfully with the following command:

units --version

Basic Usage

The units command has two main usage modes: interactive and non-interactive.

Interactive Mode

Simply enterunitscommand to enter interactive mode:

Examples

$ units
2861 units, 109 prefixes, 109 nonlinear units

You have:

After the prompt, enter the units to convert, for example:

You have: 1 mile
You want: km
        * 1.609344
        / 0.62137119

In the output:

  • *The number after * is the multiplication factor (1 mile = 1.609344 kilometers)
  • /The number after / is the reciprocal factor (1 kilometer = 0.62137119 miles)

Non-interactive Mode

You can perform the conversion directly on the command line:

units "1 mile" "km"

The output is the same as in interactive mode.


Common Unit Conversion Examples

Length Unit Conversion

Examples

# inches to centimeters
$ units "12 inch" "cm"
        * 30.48
        / 0.032808399

# feet to meters
$ units "6 feet" "meters"
        * 1.8288
        / 0.54680665

Temperature Unit Conversion

Examples

# Fahrenheit to Celsius
$ units "98.6 degF" "degC"
        * 37
        / 0.027027027

# Kelvin to Celsius
$ units "300 K" "degC"
        * 26.85
        / 0.037243908

Speed Unit Conversion

Examples

# miles/hour to kilometers/hour
$ units "60 mph" "kph"
        * 96.56064
        / 0.010356187

# knots to meters/second
$ units "15 knots" "m/s"
        * 7.7166667
        / 0.12958963

Data Storage Unit Conversion

Examples

# 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 Conversion

units can handle complex compound units:

Examples

# Convert kilowatt-hours to joules
$ units "1 kWh" "joules"
        * 3600000
        / 2.7777778e-07

# Convert pounds-force per square inch to pascals
$ units "1 psi" "pascal"
        * 6894.7573
        / 0.00014503774

Custom Units

You can~/.unitsdefine your own units in the file. For example, add a "football field" unit:

footballfield 100 m

Then you can use it:

Examples

$ units "1 footballfield" "km"
        * 0.1
        / 10

Currency Conversion (Requires Network Access)

units supports currency conversion, but requires network access to obtain the latest exchange rates:

$ units "100 USD" "EUR"

Note: The currency conversion feature may be unavailable on some systems or may require additional configuration.


Practical Tips

  1. View the available unit list: In interactive mode, enterto view help information?orhelpFuzzy search

  2. : After entering part of a unit name, press the Tab key to auto-completeHistory

  3. : In interactive mode, use the up and down arrow keys to view command historyPrecise output control

  4. : Use theparameter to display more detailed information-vCheck unit compatibility

units -v "1 lightyear" "km"
  1. : Use theparameter to only check whether units are compatible without performing conversion-cFrequently Asked Questions
units -c "joules" "watt hours"

Frequently Asked Questions

Why are some unit conversion results inaccurate?

How do I update the unit database?

How do I update the unit database?

. You can manually edit this file or wait for a system update./usr/share/units/definitions.unitsCan I perform batch conversions?

Can I perform batch conversions?

Examples

Examples

#!/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

Basic installation and usage of the units command

  1. Unit conversion techniques for common physical quantities
  2. Advanced features such as compound unit conversion and custom units
  3. Practical tips and problem-solving methods in real-world applications
  4. To gain a deeper understanding of the additional features of the units command, you can consult its manual page:

Now, try using the units command to solve the next unit conversion problem you encounter!

man units

Linux Command Reference


Linux 命令大全Other Extensions

AI is thinking...