Numfmt Command with Examples for Beginners

Today we will discuss an interesting and rather unknown team called “Numfmt”which converts the number to / from human readable format. It reads numbers in various representations and formats them in a human-readable format according to the specified parameters. If no numbers are given, it reads the numbers from standard input. It is part of the GNU Coreutils package, so you don’t have to worry about installing it. In this quick tutorial, we’ll walk you through the use of the Numfmt command with some practical examples. Numfmt command with examples

Draw a complex number like “1003040500” … This is where the Numfmt team comes in handy. Run the following command to convert the data into human readable form.

$ numfmt --to=si 1003040500
1.1G

Let’s come up with some very long and complex number, compared to the previous number. How about “10090008000700060005”? Bits are hard, aren’t they? Yes. But the Numfmt command will show the human-readable format of this number instantly.

$ numfmt --to=si 10090008000700060005
11E

Here si refers to international system of units (abbreviated as SI from Systeme Internationale, French version of the name).

Thus, if you use si, the numfmt command will automatically scale the number to match the International System of Units (SI) standard.

Numfmt also uses the following parameters too.

  • iec and iec -i – Automatic number scaling in accordance with the International Electrotechnical Commission (IEC) standard.
  • auto – With this method, the suffixes ‘K’, ‘M’, ‘G’, ‘T’, ‘P’, ‘E’, ‘Z’, ‘Y’ are interpreted as SI meanings, and Suffixes ‘Ki’, ‘Mi’, ‘Gi’, ‘Ti’, ‘Pi’, ‘Ei’, ‘Zi’, ‘Yi’ are interpreted as IEC values.
  • none – not automatic scaling.

Here are some more examples for the options above.

$ numfmt --to=iec 10090008000700060005
8.8E

$ numfmt --to=iec-i 10090008000700060005
8.8Ei

We’ve already seen how to convert a number to human-readable format. Now let’s do the opposite. Those. we will be converting numbers from human readable format. To do this, simply replace “-to” with the “-from” option as shown below.

$ numfmt --from=si 1G
1000000000

$ numfmt --from=si 1M
1000000

$ numfmt --from=si 1P
1000000000000000

We can also do it with standards iec and iec-i

$ numfmt --from=iec 1G
1073741824

$ numfmt --from=iec-i 1Gi
1073741824

$ numfmt --from=auto 1G
1000000000

$ numfmt --from=auto 1Gi
1073741824

As I said before, when using “auto”, the suffixes ‘K’, ‘M’, ‘G’, ‘T’, ‘P’, ‘E’, ‘Z’, ‘Y’ are interpreted as SI meanings, and Suffixes ‘Ki’, ‘Mi’, ‘Gi’, ‘Ti’, ‘Pi’, ‘Ei’, ‘Zi’, ‘Yi’ are interpreted as IEC values.

The Numfmt command can also be used in conjunction with other commands. Take a look at the following examples.

$ echo 1G | numfmt --from=si
1000000000

$ echo 1G | numfmt --from=iec
1073741824

$ df -B1 | numfmt --header --field 2-4 --to=si
$ ls -l | numfmt --header --field 5 --to=si

Please note that in the commands ls and df there is already a “-human readable” option for displaying results in a readable form. The above examples are for demonstration purposes only.

You can customize the output with the “-format” or “-padding” options.

5 characters, right aligned using the “-format” option:

$ du -s * | numfmt --to=si --format="%5f"

5 characters, left aligned using the “-format” option:

$ du -s * | numfmt --to=si --format="%-5f"

5 characters, right justified using the “-padding” option:

$ du -s * | numfmt --to=si --padding=5

5 characters, left justified using the “-padding” option:

$ du -s * | numfmt --to=si --padding=-5

For more features and usage, see the man page.

$ man numfmt

That’s all for now. Stay with us!

Sidebar