4

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?

4
  • 3
    Does this answer your question? How to match * with hidden files inside a directory
    – muru
    Aug 12, 2020 at 6:26
  • 1
    @muru There is also the issue of doing this non-recursively, i.e. avoiding calling du on any subdirectories.
    – Kusalananda
    Aug 12, 2020 at 6:45
  • @Kusalananda I'm not entirely sure that's what OP means by non-recursive. They could well be trying to avoid something like a naïve find . -type d -exec du -sh {} +
    – muru
    Aug 12, 2020 at 11:45
  • @muru I'm also uncertain, but Stéphane might be on the right track.
    – Kusalananda
    Aug 12, 2020 at 15:25

2 Answers 2

9

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.

2
  • 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, 2020 at 6: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, 2020 at 21:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .