Files – Difference Between File Size in ‘ls -l’ and ‘du -sh’

disk-usagefilesls

I have a most confusing questing that bothered me for years. What is the difference between the file size given by ls -l and du -sh *.

GRILL:/user/MAIL/DATA>ll
total 270
drwxr-xr-x  11 user     users         1024 Mar 21  2013 .
drwxr-xr-x   6 user     users           96 May 28  2008 ..
drwxr-xr-x  10 user     users         1024 Jun 14 09:40 Rod
drwxr-xr-x   3 user     users           96 Sep 17  2010 Atlas
drwxr-xr-x  2339 user     users       132096 Jun 14 15:00 Admin    
drwxr-xr-x   3 user     users           96 Jul 11  2014 DE
drwxr-xr-x   5 user     users           96 Jun 14 08:30 Express
drwxr-xr-x   3 user     users           96 Sep 17  2010 Deferred
drwxr-xr-x   2 user     users           96 Feb 10  2009 Imagi
drwxr-xr-x   6 user     users         1024 Jul 11  2014 NO
drwxr-xr-x   3 user     users         2048 Mar 21  2013 SE
-rw-r--r--   1 user     users           55 Mar 21  2013 cmd

GRILL:/user/MAIL/DATA>du -sk *
6723    Rod
0       Atlas
435494  Admin
2       DE
111273  Express
2       Deferred
0       Imagi
541     NO
12      SE
1       cmd

The size of Admin in ls -l is 132096, I tried removing 400000+ files from Admin directory and I didnt find the space reduced even a bit.

Whereas du -sk gives the size as 435494. Which one is the original size of the file and what is the difference between them? Could anyone please elaborate?

Best Answer

For files, ls -l file shows (among other things) the size of file in bytes, while du -k file shows the space occupied by file on disk (in units of 1 kB = 1024 bytes). Since disk space is allocated in blocks, the size indicated by du -k is always slightly larger than the space indicated by ls -kl (which is the same as ls -l, but in 1 kB units).

For directories, ls -ld dir shows (among other things) the size of the list of filenames (together with a number of attributes) of the files and subdirectories in dir. This is just the list of filenames, not the files' or subdirectories' contents. So this size increases when you add files to dir (even when files are empty), but it stays unchanged when one of the files in dir grows.

However, when you delete files from dir the space from the list is not reclaimed immediately, but rather the entries for deleted files are marked as unused, and are later recycled (this is actually implementation-dependent, but what I described is pretty much the universal behavior these days). That's why you may not see any changes in ls -ld output when you delete files until much later, if ever.

Finally, du -ks dir shows (an estimate of) the space occupied on disk by all files in dir, together with all files in all of dir's subdirectories, in 1 kB = 1024 bytes units. Taking into account the description above, this has no relation whatsoever with the output of ls -kld dir.

Related Question