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.
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.
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).
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:02ls
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:58ls
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:11du
command computes the sum of the sizes of the files under a given directory. – Keith Thompson Apr 18 '13 at 17:23