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.
numfmt [OPTION]... [NUMBER]...
Try it
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.
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
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:
simeans 1 K = 1000,iecmeans 1 K = 1024. In IEC mode the suffix isK,M, ... withoutiunless you useiec-i. That is whyls -lhanddu -hshowKfor 1024. - Rounding goes up by default, so
numfmt --to=iec 1025shows1.1Kinstead of1.0K. Use--round=nearestfor 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-babove). Use-dfor tab-separated or comma-separated data. - Headers:
df | numfmt --header --field=2-4 --to=iec --from-unit=1024converts the numeric columns ofdfwhile leaving the title line alone. - Suffix parsing is case-sensitive for the unit letter:
Kandkboth work for kilo, but trailingBoriBare 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:
awkwith a loop dividing by 1024, ordu -h,ls -lhanddf -h, which already format sizes.
Related
printf,awk,bc,seq- Lessons: Disk usage, printf and date