How to get the size of a folder as reported by Windows Explorer on Linux?

On Windows when you view the properties of a folder the size is calculated as follows (recursively):

  • for files the length of the content counts
  • symbolic links count zero
  • folders themselves count zero

enter image description here

Is it possible to get the same result on the Linux command line?

Here are some commands from various SE posts which I already tried:

du -s -B 1 folder
du -sb folder
du -sh --apparent-size folder
tree folder -s

The problem is they all count 4k for folders. Anyone has an idea?

Asked By: zomega


Essentially, you only want to count the lengths of the contents of “real” files inside the directory; with GNU find:

find /path/to/directory -type f -printf '%sn' | awk '{ s += $1 } END { print s }'

This finds all files under the named directory, outputs their apparent size, and calculates the sum using AWK.

Answered By: Stephen Kitt
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.