Linux units Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux units command, a versatile tool for converting between different units of measurement. The units command allows you to perform unit conversions and arithmetic operations involving various units, such as length, mass, time, and more, making it particularly useful for scientific, engineering, and everyday calculations.

We will start by understanding the basic usage of the units command, including how to convert between different units and perform arithmetic operations with units. Then, we will focus on converting between different time units, such as hours, minutes, days, and seconds, as well as milliseconds and microseconds.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux/BasicSystemCommandsGroup -.-> linux/bc("`Arithmetic Calculations`") linux/SystemInformationandMonitoringGroup -.-> linux/date("`Date/Time Displaying`") linux/TextProcessingGroup -.-> linux/expr("`Evaluate Expressions`") subgraph Lab Skills linux/bc -.-> lab-422977{{"`Linux units Command with Practical Examples`"}} linux/date -.-> lab-422977{{"`Linux units Command with Practical Examples`"}} linux/expr -.-> lab-422977{{"`Linux units Command with Practical Examples`"}} end

Understand the units Command

In this step, we will explore the units command in Linux, which is a versatile tool for converting between different units of measurement.

The units command allows you to perform unit conversions and arithmetic operations involving various units, such as length, mass, time, and more. It can be particularly useful for scientific, engineering, and everyday calculations.

To get started, let's install the units package in our Ubuntu 22.04 Docker container:

sudo apt-get update
sudo apt-get install -y units

Now, let's explore some basic usage of the units command:

## Launch the units interactive mode
units

## Convert between units
> meter to foot
* 3.2808399
/ 0.3048

## Perform arithmetic operations with units
> 5 kilogram + 10 pound
result is 12.27272727 kilogram

## Exit the units interactive mode
Ctrl + D

Example output:

Currency exchange rates not available.
You have: meter
You want: foot
* 3.2808399
/ 0.3048

You have: 5 kilogram
You want: 10 pound
result is 12.27272727 kilogram

The units command provides a comprehensive database of units, allowing you to convert between a wide range of measurements. In the interactive mode, you can enter the units you want to convert from and to, and the command will provide the conversion factor.

Additionally, you can perform arithmetic operations with units, which can be particularly useful for scientific and engineering calculations.

Convert Between Different Time Units

In this step, we will learn how to use the units command to convert between different time units.

Let's start by launching the units interactive mode again:

units

Now, let's try some time unit conversions:

## Convert hours to minutes
> 5 hour to minute
* 300

## Convert days to seconds
> 7 day to second
* 604800

## Convert milliseconds to microseconds
> 500 millisecond to microsecond
* 500000

Example output:

You have: 5 hour
You want: minute
* 300

You have: 7 day
You want: second
* 604800

You have: 500 millisecond
You want: microsecond
* 500000

As you can see, the units command makes it easy to convert between various time units, such as hours, minutes, seconds, days, milliseconds, and microseconds.

You can also perform arithmetic operations with time units:

## Add 2 hours and 30 minutes
> 2 hour + 30 minute
result is 2.5 hour

## Multiply 3 days by 12 hours
> 3 day * 12 hour
result is 36 hour

Example output:

You have: 2 hour
You want: 30 minute
result is 2.5 hour

You have: 3 day
You want: 12 hour
result is 36 hour

The units command provides a comprehensive database of time units, allowing you to easily convert and perform calculations with them.

Perform Arithmetic Operations with units

In this final step, we will explore how to perform arithmetic operations using the units command.

The units command allows you to perform a variety of mathematical operations on units, including addition, subtraction, multiplication, and division. This can be particularly useful for scientific and engineering calculations.

Let's start by launching the units interactive mode again:

units

Now, let's try some arithmetic operations with units:

## Add 5 meters and 10 feet
> 5 meter + 10 foot
result is 7.6248 meter

## Subtract 3 kilograms from 10 pounds
> 10 pound - 3 kilogram
result is 4.8181818 pound

## Multiply 2 miles by 3 hours
> 2 mile * 3 hour
result is 6 mile_hour

## Divide 100 kilometers by 2 hours
> 100 kilometer / 2 hour
result is 50 kilometer/hour

Example output:

You have: 5 meter
You want: 10 foot
result is 7.6248 meter

You have: 10 pound
You want: 3 kilogram
result is 4.8181818 pound

You have: 2 mile
You want: 3 hour
result is 6 mile_hour

You have: 100 kilometer
You want: 2 hour
result is 50 kilometer/hour

As you can see, the units command allows you to perform a wide range of arithmetic operations on units, including addition, subtraction, multiplication, and division. The result is always displayed in the appropriate unit.

This can be particularly useful for complex calculations involving different units, as the units command handles the unit conversions automatically.

Summary

In this lab, we first explored the units command in Linux, which is a versatile tool for converting between different units of measurement. We learned how to install the units package, launch the interactive mode, and perform basic unit conversions and arithmetic operations. We then focused on converting between different time units, such as hours to minutes, days to seconds, and milliseconds to microseconds, using the units command.

The units command provides a comprehensive database of units, allowing us to easily convert between a wide range of measurements. It can be particularly useful for scientific, engineering, and everyday calculations.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like