[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [coreutils] "ls -lh" rounds up the size of files instead of rounding
Re: [coreutils] "ls -lh" rounds up the size of files instead of rounding half up/down.
Mon, 10 Jan 2011 13:16:06 -0800 (PST)
Can you add an option to round half up/down?
There are times when users want to see close approximation rather than safely
rounded up sizes.
----- Original Message ----
From: Eric Blake <address@hidden>
To: crocket <address@hidden>
Sent: Tue, January 11, 2011 5:43:35 AM
Subject: Re: [coreutils] "ls -lh" rounds up the size of files instead of
rounding half up/down.
On 01/10/2011 01:30 PM, crocket wrote:
> There is a file whose size is 42949837312bytes.
> It's almost exactly 40.000153064727783GB.
> "ls -lh" displays it as 41GB.
> It is resulted due to rounding up.
> However, rounding half down/up is much more common than rounding up in the
> When I expect moderate file size approximation, this 1GB difference is not
> Is there any reason for this behavior?
Yes. -h is a GNU extension, but the comparable -k and -s options have
behavior specified by POSIX. And POSIX requires that these options
round up to the next integral number of units. Therefore, it is as if
once you cross a unit boundary, ls is treating that unit as the block
size and rounding up to the effective number of blocks of that unit
size, since you've lost the fractional portion (that is, it's better to
overestimate than underestimate when it comes to disk usage).
under STDOUT: "If any of the -l, -n, -s, [XSI] [Option Start] -g, or -o
[Option End] options is specified, each list of files within the
directory shall be preceded by a status line indicating the number of
file system blocks occupied by files in the directory in 512-byte units
if the -k option is not specified, or 1024-byte units if the -k option
is specified, rounded up to the next integral number of units".
> Or is it just a mistake?
No, it is intentional.
Eric Blake address@hidden +1-801-349-2682
Libvirt virtualization library http://libvirt.org