What is the difference between file size in ls -l and du-sh?
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
.