linux - How does `ls -lh` round file size? -


i'm comparing rounded file size value displayed ls -lh raw size in bytes (as displayed ls -l, say). i'm having hard time figuring out algorithm uses conversion bytes.

my assumption interprets units k,m,g either

  • (a) 10^3, 10^6, 10^9, or
  • (b) 1024, 1024^2, 1024^3.

on 1 hand, have 1 file ls -l reports 2052 bytes, , ls -lh rounds 2.1k:

$ ls -l usercount.c  -rw-r--r-- 1 ssanjabi lsf 2052 may 13 15:41 usercount.c $ ls -lh usercount.c  -rw-r--r-- 1 ssanjabi lsf 2.1k may 13 15:41 usercount.c 

this seem support hypothesis (a), because 2052/1000=2.052 rounds 2.1k 2052/1024=2.0039 display 2.0k when rounded 1 decimal place.

on other hand, have file ls -l reports being 7223 bytes, ls -lh displays 7.1k:

$ ls -l traverse.readdir_r.c -rw-r--r-- 1 ssanjabi lsf 7223 jul 21  2014 traverse.readdir_r.c $ ls -lh traverse.readdir_r.c -rw-r--r-- 1 ssanjabi lsf 7.1k jul 21  2014 traverse.readdir_r.c 

this confusingly supports hypthesis (b), because 7223/1000=7.223 should round down 7.2k, 7223/1024=7.0537 rounds displayed 7.1k

this leads me conclude assumption wrong , neither (a) nor (b) exclusively. algorithm ls use rounding?

gnu ls default round up in 1024-based units.

it not round nearest, you've taken granted.

here's formatting flag gnulib human.h:

/* round plus infinity (default).  */ human_ceiling = 0, 

this consistent you're seeing:

  • 2052 2.0039 kib rounds 2.1
  • 7223 7.0537 kib rounds 7.1

Comments