What does du command show?

4.6k views Asked by At

I have a normal folder and so when I right click, go to properties and check its data size, it gives 37.3KB. But when I use the du command, it shows 104KB

Shouldn't these two give the same answer?

3

There are 3 answers

5
Steve Summit On

The du command recursively descends all directories and subdirectories and always gives you the total contained anywhere beneath.

The properties tab in a GUI doesn't always do this. Sometimes there's a checkbox (there, or in some other preferences dialog) controlling whether folder sizes are computed recursively, or only give you the top level. Sometimes, even when the UI does try to compute the size recursively, it takes it a while to update it, so it may show a smaller, temporary number for a while, until it gets around to completing the full recursive search.

So it's not surprising if the UI is giving a smaller number. Try finding the UI preference for this.

0
Thomas Dickey On

The answer by @Steve Summit along with the followup comments overlook some useful information for understanding the problem:

  • a simple ls -ls will not show hidden subdirectories.
  • you should see the names of any hidden subdirectories using ls -las.
  • to see their size recursively, add the -R option.
  • a better way to show the contents would be with find . -ls, which does show subdirectories.
  • in either case, du shows blocks, which hold "many" bytes
  • POSIX's verbiage for blocksize (see rationale in ls) says that the result of -s differed across implementations (some may show 1024 for instance):

    Earlier versions of this standard did not have the -k option, which meant that the -s option could not be used portably as its block size was implementation-defined, and the units used to specify the number of blocks occupied by files in a directory in an ls -l listing were fixed as 512-byte units.

  • On the other hand, the standard for du says it should be 512-byte units, just like ls -l.

To account for the missing space:

  • If you have a lot of very small files, they can use far fewer bytes than the number of blocks.
  • Likewise, you may have hidden files which were not accounted for in the question.
  • Finally, some properties dialogs may show only files. Directories also take up space.

By the way, the -ls option of find is mentioned in the standard only in the rationale. But it works with Linux.

0
Adrien Nader On

It is important to note that hardlinks matter when computing the size of a directory. Two hardlinks to the same file don't take more disk space and share inode number (by definition) but a simple count of file sizes will count them in double.

There's this switch to at least GNU du:

   -l, --count-links
          count sizes many times if hard linked