Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Text Processing

Linux numfmt command

Reference for the Linux numfmt command: convert numbers to and from human-readable units like 1.5K or 2.0M, SI versus IEC, fields, padding and rounding.

numfmt (number format) converts numbers between plain digits and human-readable forms with unit suffixes. numfmt --to=iec 1536 gives 1.5K, and numfmt --from=iec 2M gives 2097152. It can work on command-line arguments or on a chosen field of every line read from standard input, which makes it a good filter for du, df and ls -l style output.

numfmt is part of GNU coreutils 8.21 and later. It may be missing on very old systems and on BSD or macOS. The examples first check that the command exists; if it does not, they print the expected output instead, so treat the printed numbers as the documented behavior.

Terminal
numfmt [OPTION]... [NUMBER]...

Try it

Bash To and from human-readable (read-only if numfmt is missing)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Fields, padding and rounding

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
numfmt [OPTION]... [NUMBER]...
numfmt [--from=UNIT] [--to=UNIT] [--field=N] [--round=METHOD] [--padding=N] [--format=FMT] [--suffix=S] [--header[=N]] [--invalid=MODE]
Option Description
--to=UNIT Scale numbers to a unit system: none, si (powers of 1000, K M G), iec (powers of 1024, K M G), iec-i (Ki Mi Gi), auto.
--from=UNIT Interpret input suffixes using the same unit systems.
--from-unit=N, --to-unit=N Size of the input or output unit, in bytes (for example --from-unit=1024 for numbers counted in KiB).
--field=N Convert field N (or a list, or -) of each input line; default is the first.
-d, --delimiter=X Field separator; default is whitespace runs.
--round=METHOD up (default), down, from-zero, towards-zero, nearest.
--padding=N Pad the output to width N; negative values left-align.
--format=FMT Use a %f-style format such as %.2f; ' adds a thousands separator.
--grouping Group digits by thousands according to the locale.
--suffix=S Add S after each number (for example B).
--header[=N] Skip the first N lines (default 1) unchanged.
--invalid=MODE What to do with bad input: abort (default), fail, warn, ignore.

Returns: 0 on success; non-zero when some input could not be converted (unless --invalid=ignore).

Usage notes

  • SI versus IEC: si means 1 K = 1000, iec means 1 K = 1024. In IEC mode the suffix is K, M, ... without i unless you use iec-i. That is why ls -lh and du -h show K for 1024.
  • Rounding goes up by default, so numfmt --to=iec 1025 shows 1.1K instead of 1.0K. Use --round=nearest for what people expect on dashboards.
  • Only one digit after the point is shown for values under 10 and none above, which can hide detail. Use --format='%.2f' for more.
  • Whitespace and fields: with the default delimiter, the spacing around the converted field can change (note the extra spaces after file-b above). Use -d for tab-separated or comma-separated data.
  • Headers: df | numfmt --header --field=2-4 --to=iec --from-unit=1024 converts the numeric columns of df while leaving the title line alone.
  • Suffix parsing is case-sensitive for the unit letter: K and k both work for kilo, but trailing B or iB are accepted only in some positions. Strip them first if needed.
  • Precision: results come from floating-point arithmetic of long double precision, so extremely large values lose exactness.
  • Not for scripts that compare sizes: keep raw bytes for comparisons and only format when printing.
  • Alternative without numfmt: awk with a loop dividing by 1024, or du -h, ls -lh and df -h, which already format sizes.