Session and utilities

The numfmt command

Converts numbers to a readable form (1M, 2G) or back. Handy for sizes.

Syntax

numfmt [opciones] [número...]Try it in the terminal →

Most useful options

--to <unidad>Scales the output: iec (K, M, G in base 1024), si (base 1000) or iec-i (Ki, Mi, Gi)
--from <unidad>Accepts suffixes in the input: with iec, 1K is 1024; with si, it is 1000
--field <campos>Converts only those columns, using cut syntax: 2, 2-4, -3
--header [N]Prints the first N lines unchanged, 1 by default. Useful for table headers
--padding <N>Pads the output to N characters. With a negative N, aligns left
--invalid <modo>What to do with an invalid number: abort (default), fail, warn or ignore

Copy-ready examples

$ numfmt --from=iec 1Mfrom 1M to bytes: the reverse direction.

Example output

1048576
$ numfmt --to=iec 1048576bytes to a readable form, in base 1024.

Example output

1.0M
$ numfmt --from=si --to=iec 1500000converts from base 1000 to base 1024 in one command.

Example output

1.5M
$ numfmt --to=iec --format='%.2f' 1048576sets two decimals with a printf format.

Example output

1.00M
$ printf 'tamano\n2048\n' | numfmt --header --to=iecconverts the column of numbers and leaves the header as it is.

Example output

tamano
2.0K

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
numfmt: invalid number: 'abc'The input is not a number.Check the input. With --invalid=warn, the value is left as it is and only a warning is shown.
numfmt: invalid suffix in input: '1X'The input has a suffix that the --from unit does not recognise.Use a valid suffix, such as 1K or 1M with --from=iec.
numfmt: invalid argument 'foo' for '--to'The --to value does not exist. The valid ones are none, si, iec and iec-i.Use one of those values, for example --to=iec.

Typical mistakes

  • With --to=si, the suffix may come out as k or K depending on the coreutils version. Check it before reading it in a script.
  • Without --from, suffixes in the input are an error: 1K is not read as a number until you give the unit.
  • By default it converts only the first field of each line. For another column, use --field.

Differences from similar commands

  • ls: with -h it shows readable sizes directly. numfmt is for numbers you already got from another command.
  • du: measures folder space and, with -h, already gives a readable format. numfmt formats the numbers you pass it.
  • awk: can format numbers with printf, but needs code. numfmt does it with a single command.

Related commands

FAQ

How do I change the number of decimals?

With --format in printf style: --format='%.2f' gives two decimals. The format must contain a single %f.

Does it work with decimals in the input?

Yes. With --from=iec, 1.5K gives 1536, because the decimal part is multiplied by the unit.

Why does it print 1.0M and not 1M?

When scaling, numfmt shows one decimal if the value is below 10. From there on it shows none, as in 15M. If you want no decimals, use --format='%.0f'.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course