Linux – Difference Between ‘du -h’ and ‘ls -lh’ Commands

dulinuxls

I am having a difficult time grasping what is the correct way to read the size of the files since each command gives you varying results. I also came across a post at http://forums.devshed.com/linux-help-33/du-and-ls-generating-inconsistent-file-sizes-42169.html which states the following;

du gives you the size of the file as it resides on the file system. ( IE will will always give you a result that is divisible by 1024 ).

ls will give you the actual size of the file.

What you are looking at is the difference between the actual size of the file and the amount of space on disk it takes. ( also called file system efficiency ).

What is the difference between as it resides on the file system and actual size of the fil

Best Answer

This is called slack space:

Each layer of abstraction on top of individual bits and bytes results in wasted space when a datafile is smaller than the smallest data unit the file system is capable of tracking. This wasted space within a sector, cluster, or block is commonly referred to as slack space, and it cannot normally be used for storage of additional data. For individual 256-byte sectors, the maximum wasted space is 255 bytes. For 64 kilobyte clusters, the maximum wasted space is 65,535 bytes.

So, if your filesystem allocates space in units of 64 KB, and you store a 3 KB file, then:

  • the file's actual size is 3 KB.
  • the file's resident size is 64 KB, as the remaining 61 KB in that unit can't be allocated to another file and is thus lost.

Note: Some filesystems support block suballocation, which helps to mitigate this issue by assigning multiple small files (or the tail ends of large files) into the same block.