1

This question already has an answer here:

Always when I run ls -ld /dir the output give me the size = 512 even if there are files in it or not.

but when I run du -sh the output give me the actual size of the directory including its content.

marked as duplicate by Gilles, vonbrand, jasonwryan, Mat, manatwork Apr 19 '13 at 11:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

Actually if you would have many files in your directory you would get a larger number there: when I create 10000 empty files in a new directory that number goes from 4096 to 262144.

The starting size is depending on the filesystem and blocksize as specified when creating the filesystem. It is an indication of how much metadata the directory is holding (for the files and directories contained in it), not how much data the files in a directory are holding.

To compare: A directory with 10000 empty files

 ls -ld . --> 262144
 du -sh . --> 260K

A directory with 10000 files of 100000 bytes each:

 ls -ld . --> 262144
 du -sh . --> 977M

Metadata is the same size for both set of files (they also have exactly the same names).

  • But what I know that ls command give you the actual size of the file on the disk, not the allocated size on the disk. du command gives you the allocated size. – MUE Apr 18 '13 at 15:02
  • What do you mean with allocated size? It is the size of the directory, which is not the cumulative size of the files listed in that directory or any of its subdirectories. – Anthon Apr 18 '13 at 15:44
  • did you mean that ls doesn't calculate the size of files and subdirectories in the directory. allocated size: how many blocks for each file or directory allocated to it by the OS. – MUE Apr 18 '13 at 15:58
  • ls does not calculate the sum of the sizes of files pointed to by it. It just shows the directory size which is the metadata. I extended my answer to show the difference of 2 directories with empty files and with substantial files. with the same names and count of files the directories of course have the same size, as they contain the same metadata information. – Anthon Apr 18 '13 at 16:11
  • 3
    Files "under" a directory are not contained in the directory; rather, the directory refers to the files. A directory is similar to an ordinary file. It contains an entry for each file "under" it, and that entry contains the file's name and some metadata, including a reference to the file's inode. The du command computes the sum of the sizes of the files under a given directory. – Keith Thompson Apr 18 '13 at 17:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.