7

I have a directory with a few hundred files (real files, no symlinks, no subdirectories).

When I use ls -la and sum up the sizes in Excel I get 287190 bytes(?).

When I use du -b or du --apparent-size --block-size=1 I get 422358 bytes(?).

I thought those two things mean the same thing, why the difference?

3

2 Answers 2

6

du gives disk usage, which is not the same as the sum of all the file sizes.

For example, a du -b file will give a different output than making a directory "dir", placing the same file in "dir" and doing du -b dir. On my system that's 30 extra bytes for the "overhead" of a directory. Depending on the contents of the directory, I imagine the directory size would change (but I would be surprised if it was perfectly linear).

Also, the relative size of the difference implies that you might have missed a hidden directory with quite a few files in it, or that you might have missed a lot of hidden files (even though you did use the -a flag).

In addition, there may be symlinks which cause differences if one tool follows them while the other doesn't.

Finally, with some file systems, if the file's contents are small enough they might get inlined into the file system INode, and with many file systems, a single block is reserved to hold the contents of the file even if that block is not fully used. These variations add extra noise when attempting to compare the two.

1
  • "For example, a du -b file will give a different output than making a directory "dir", placing the same file in "dir" and doing du -b dir. On my system that's 30 extra bytes for the "overhead" of a directory." <- This is probably it. An empty directory itself needs 4096 bytes apparent size.
    – AndreKR
    Commented Dec 4, 2015 at 20:26
0

An hard drive is divided into crusters. Each file occupy one or several cluster.

Imagine an hard drive with 16 bytes clusters.

A file of 12 bytes will occupy 1 cluster, with some bytes not used in the cluster. with ls, the file size will be 12, with du, it will be 16.

A file of 32 bytes will occupy excatly 2 clusters. with ls, the file size will be 32, with du, it will be 32.

Do this for all files, sum up, you will have the values you wrote.

More info: https://en.wikipedia.org/wiki/Data_cluster

2
  • What is the point of --apparent-size then?
    – AndreKR
    Commented Dec 4, 2015 at 20:22
  • And which command do I have to use if I want to see the exactly same file size value (real size) like displayed in file browser via "right click" ⇨ "properties"?
    – Dave
    Commented Sep 28, 2017 at 5:45

You must log in to answer this question.

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