Subject: Re: bug#22363: Bug of stat in bash
From: Eric Blake <[email-masked]>
To: Arturo <[email-masked]>, [email-masked]
Date: Wed, 13 Jan 2016 10:24:20 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

tag 22363 notabug
thanks

On 01/13/2016 08:14 AM, Arturo wrote:
> Hi.
> I've needed isolate the size of a directory writing a script. I've used "stat -c %s directory", but the result is a great surprise. You can view the image stat.jpg: the size of /var/cache/apt/archives is 9804 KB (10 MB aprox.), but stat shows "159744". This number is the same if the directory have 60 MB, 10 MB or 0 MB. In other equal instalation (Ubuntu 14.04) this number showed 4096 with more than 300 MB in the same directory.
> In the stat2.jpg image you can view the same situation with the directory empty.

In the future, please don't spam the list with a .jpg screenshot of a
terminal, when a simpler text-based copy-and-paste of its contents will
convey the same information with a lot less bandwidth.  Remember, ever
100kb you send in a .jpg turns into megabytes of outgoing traffic when
it is multiplied to all list subscribers.

> It's a bug?

No. The size of a directory is heavily dependent on the file system in
use, but in general is a multiple of the file system sector size (for
example, if the minimum size you can allocate in a file system is 64k,
but the information required to describe a single entry in a directory
occupies only 512 bytes, then a directory in that file system would show
up as 64k in size if it has anywhere between 1 and 128 entries, and
would jump to 128k in size if it has between 129 and 256 entries).

The 'stat' program is merely reporting to you the information reported
by the operating system, which in turn is reporting what the file system
is using.  It is NOT coreutils' fault that the number reported is not
the number you are looking for.  In other words, when you are asking
stat for the size of the directory, you are getting how much space the
directory itself occupies on the disk, and NOT recursing into how much
space its contents occupy.

> The way to obtain my goal (only the number) is another?

It sounds like what you are asking for is the cumulative size of all
files within a directory, added together, and NOT how much space the
directory itself occupies on disk.  The program for that is 'du'
(mnemonic for 'disk usage'), not 'stat'.

Maybe we are missing a link on the 'stat' manual page pointing to 'du',
but other than that, I see no problem here, so I'm closing the report.
However, feel free to reply with any further related questions.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org