5

I have a parent directory somedir containing two child directories .hiddenDir and notHidden.

I want to non-recursively measure the sizes of all child directories in somedir.

How can I do this in Unix?


picture of what I want:

enter image description here

failed attempt:

The following du based command only returns the visible directories:

du -sh *

While

du -sh .*

only returns the hidden directories.


How can I return the sizes of all the directories like in the picture?

muru
  • 72,889
Conor
  • 179

2 Answers2

12

With GNU du:

du -hd1

(it also reports the disk usage for . which will be the sum of the disk usages reported for the subdirectories, and that of the non-directory files in the current directory).

With zsh and any du:

du -sk -- *(/D)

(in kibibytes).

In any case, while that doesn't report the disk usage of directories at depth 2 or below and their contents, the disk usage of those are still included in the disk usage reported by for the top level directories (in other word, there is recursion).

That's one of the potential meaning of the size of a directory. In other words, that's the disk space that would be reclaimed if that directory and its contents were to be removed (assuming the files have no hard links outside those directories, and that their data is not referenced (possibly in part) in other files outside those directories), note that it's generally different from the sum of the apparent size of the files themselves (including directory files) as reported by ls -l.

2

Du takes multiple arguments - in fact the *'s are expanded by your shell before they're given to du. So this is all you need to do.

du -sh * .??*

Caveat, this will miss files or directories named with . and a single character - for example, a directory called .a.

.??* is used to skip . and .., which are links the the current and parent directories.

  • 1
    This seems to miss any name with a single character after the first dot, e.g. .a. It would additionally measure the sizes of all matching directories recursively, not just to the 1st level. – Kusalananda Aug 12 '20 at 06:16
  • good point about .a, text updated. I don't think that you understand what the OP meant about non-recursively though. – Dan Pritts Aug 12 '20 at 21:48