Search code examples
javalinuxapache-commons-io

Apache commons-io get directory size - mismatch with du


I try to find out the size of a directory in Java. I check it by Linux bash.

POM

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.18.0</version>
</dependency>

Java

Path p = Path.of ("mydir");
long sz = FileUtils.sizeOf (p.toFile ());

Console

du -sch mydir

du gives me 3.0M, but commons-io tells me 2168168 (that's a bit more than 2MB).

Where is the problem here?


Solution

  • Because du -sch measure the space taken on the disk, if you want to measure the actual directory size use du -sch --apparent-size mydir