3

I am using the du command in order to get the size of the folder. As I am interested in the actual size I ended up with the following command:

du –sh --apparent-size <someFolder>/

In this examples this returns 4.6M. However I know that the folder actually has exactly 4M and WinSCP confirms it (4096KiB).

So what command would I have to use to get "4M" as a result (Preferrably in a human readable format)?

Also please be aware that I also need to include all subfolders so that it has to be recursive. Folder structure is something like this:

_cache
+-- _fold1
+-- _fold2
|   +-- _fold2.1
|       +-- file2.1.1
|       +-- file2.1....
|       +-- file2.1.5000
|   +-- _fold2.2
|       +-- file2.2.1
|       +-- file2.2....
|       +-- file2.2.5000

Thanks!

7
  • try du -sk or du -sh without the --apparent-size modifier.
    – MelBurslan
    Feb 10, 2016 at 14:48
  • without the modifier it returns a folder size of approx. 1G. Although this might be the actual "size on disk" this is not the "real file size".
    – newBee
    Feb 10, 2016 at 14:51
  • try running this command and see what it gives you a=0;find some_dir_name_here -type f | xargs ls -l| while read line; do b=$(echo $line|awk '{print $5}'); (( a=$a+$b ));done; echo "Size is " $a
    – MelBurslan
    Feb 10, 2016 at 14:59
  • Despite running very very long it always returns 0. Maybe should have mentioned that the folder contains subfolders, so it has to be recursive.
    – newBee
    Feb 10, 2016 at 15:07
  • find is recursive from the top level directory name you give, unless it is explicitly told the depth. Does this folder contain a lot of so-called sparse files by any chance ? Like database table containers and such ?
    – MelBurslan
    Feb 10, 2016 at 15:20

1 Answer 1

2

The command du is intended to show disk usage. Disk usage for a directory includes the size the actual directory takes.

A directory is a special type of file that holds the names and inodes of all the files or other entries in it. This takes up disk space.

For example, I have created three directories.

One is dir1, which contains a single file sized 40M. The directory dir2 contains ten subdirectories, each of which contains a single file sized 4M. The directory dir3 contains ten thousand empty files.

Running du -s -B 1 dir1 dir2 dir3 gives

41947136    dir1
41988096    dir2
258048  dir3

For small directories, a directory size (at least on an ext4 filesystem without any exotic settings) is 4096 bytes. For large directories (containing many files) the size is larger (as ls -l would tell you). Sum up the sizes of the directories together with the files, and I believe you will end up with the difference you are witnessing.

2
  • This was the missing part in my logic: For large directories (containing many files) the size is larger. When substracting the size of the folders itself I get the expected, exact 4096KiBs Thank you
    – newBee
    Feb 10, 2016 at 16:21
  • also if you filesystem has journalling the size can explode, if you want to see the real size of a dataset then du fails
    – magor
    Sep 3, 2020 at 8:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .