Categories
Uncategorized

How to pretty print decimal or binary numbers

Use numfmt command-line utility which is a part of the standard GNU Core Utilities to pretty print decimal or binary numbers.

Pretty print binary number.

$ echo 2K | numfmt --from=iec
2048

Pretty print decimal number.

$ echo 2K | numfmt --from=si
2000

Pretty print binary number using bytes and GiB.

$ numfmt --from=iec 1G
1073741824
$ numfmt --from=iec --to=iec 1073741824
1.0G

Pretty print number of open files.

$ lsof | wc -l
207759
$ lsof | wc -l | numfmt --to=si
207K

Pretty print memory usage of specific process.

$ sudo pmap 7365 | tail -1
 total           226396K
$ sudo pmap 7365 | tail -1 | numfmt --from=iec --field=2 --to=iec
 total              222M

Pretty print file sizes using defined padding.

$ du --summarize --apparent-size --human  *
3,0K    haproxy.cfg
1,1G    disk.vdi
384     stats-config.lua
21K     stats.lua
6,3K    stats.sh
$ du --summarize --apparent-size --bytes  * | numfmt --to=iec --padding=-7
3,0K    haproxy.cfg
1,1G    disk.vdi
384     stats-config.lua
21K     stats.lua
6,3K    stats.sh

Pretty print available disk space by replacing specific fields.

$ df -h
Filesystem                      Type      Size  Used Avail Use% Mounted on
tank/lxd/containers/development zfs       791G  1.2G  790G   1% /
none                            tmpfs     492K     0  492K   0% /dev
udev                            devtmpfs   16G     0   16G   0% /dev/fuse
tmpfs                           tmpfs     100K     0  100K   0% /dev/lxd
tmpfs                           tmpfs     100K     0  100K   0% /dev/.lxd-mounts
tmpfs                           tmpfs      16G     0   16G   0% /dev/shm
tmpfs                           tmpfs      16G  132K   16G   1% /run
tmpfs                           tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs                           tmpfs      16G     0   16G   0% /sys/fs/cgroup
tmpfs                           tmpfs     3.2G     0  3.2G   0% /run/user/1983
$ df -BK
Filesystem                      Type      1K-blocks     Used  Available Use% Mounted on
tank/lxd/containers/development zfs      829321984K 1212416K 828109568K   1% /
none                            tmpfs          492K       0K       492K   0% /dev
udev                            devtmpfs  16439564K       0K  16439564K   0% /dev/fuse
tmpfs                           tmpfs          100K       0K       100K   0% /dev/lxd
tmpfs                           tmpfs          100K       0K       100K   0% /dev/.lxd-mounts
tmpfs                           tmpfs     16456228K       0K  16456228K   0% /dev/shm
tmpfs                           tmpfs     16456228K     132K  16456096K   1% /run
tmpfs                           tmpfs         5120K       0K      5120K   0% /run/lock
tmpfs                           tmpfs     16456228K       0K  16456228K   0% /sys/fs/cgroup
tmpfs                           tmpfs      3291244K       0K   3291244K   0% /run/user/1983
$ df -BK | numfmt --header --from=iec --field 3-5 --header=1 --to=iec
Filesystem                      Type      1K-blocks     Used  Available Use% Mounted on
tank/lxd/containers/development zfs            791G     1.2G       790G   1% /
none                            tmpfs          492K        0       492K   0% /dev
udev                            devtmpfs        16G        0        16G   0% /dev/fuse
tmpfs                           tmpfs          100K        0       100K   0% /dev/lxd
tmpfs                           tmpfs          100K        0       100K   0% /dev/.lxd-mounts
tmpfs                           tmpfs           16G        0        16G   0% /dev/shm
tmpfs                           tmpfs           16G     132K        16G   1% /run
tmpfs                           tmpfs          5.0M        0       5.0M   0% /run/lock
tmpfs                           tmpfs           16G        0        16G   0% /sys/fs/cgroup
tmpfs                           tmpfs          3.2G        0       3.2G   0% /run/user/1983

Print number of blocks using specified block size.

$ numfmt --from=iec --to=iec --to-unit=4096 8192M
2.0M
$ numfmt --from=iec --to=iec --from-unit=4Ki --format="%.2f" 12
48.00K
$ numfmt --from=iec --to=iec --from-unit=512 --format="%.2f" 3
1.50K

Print real disk capacity for 256G hard disk.

$ numfmt --from=si --to=iec --format="%.1f" 256G
238.5G

Print real disk capacity for 1T hard disk.

$ numfmt --from=si --to=iec --format="%.1f" 1T
931.4G