16

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?

2

2 Answers 2

8

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.

3
  • About the non-reclaiming, that is true of most FS with traditional structures, but not of some modern file systems like ZFS or BTRFS. Commented Jun 15, 2017 at 6:26
  • 1
    Also a correction on the size indicated by du is always slightly larger than the space indicated by ls, sparse files will have less space used on disk (and thus reported by du) depending on the size and number of holes. I'm unsure if du is also capable of taking into account/recognising filesystem-level block or file compression, which would make ls further report a larger logical filesize over dus physical usage.
    – Hashbrown
    Commented Apr 3, 2023 at 13:45
  • @Hashbrown not only sparse files but transparently compressed files and resident/inline files (for example in Btrfs, ext4 and NTFS) will also consume less space on disk so will have du reported smaller sizes. You'll need du --apparent-size to get the actual size
    – phuclv
    Commented Jul 9, 2023 at 5:21
0

ls -l only shows file sizes at 1 level.

du will show folder sizes recursively (sum of file sizes under the folder).

You must log in to answer this question.

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