5

I have the following directory structure (example) -

main
|--src
    |--com
        |- company
    |--org
        |- apache
|--resources
    |--abc
|--etc

I need the disk space used by each sub-directory under the main directory. So, the output would be something like -

user@main> du -{command_switches}
1M  src
20M resources
3M  etc

I have tried several switches available - sh, Sh, ash, aSH - but I could not get the required result.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Rakesh N
  • 153
  • 2
    If you don't need that information put it a file or something like that, you could try ncdu which shows what you need in an interactive TUI environment. – Mio Rin Apr 27 '18 at 13:26

3 Answers3

10

Some du implementations support -d¹ to limit the depth at which disk usage is displayed (not at which disk usage is accounted), so du -hd 1 . should work for the current directory.

Portably, you can always do:

find . ! -name . -prune -type d -exec du -s {} +

(add -h if your du implementation supports it)

Though note that if there are a lot of directories in the current directory, find may end up running several invocations of du which could mean that some hard links are counted several times. Some du implementations also don't prevent hard links from being counted several times if they're encountered through the traversal of different arguments.


¹, with older versions of GNU du, you may need --max-depth instead. The -d short option equivalent was only added in coreutils 8.8 for compatibility with FreeBSD

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
emmrk
  • 369
3

You may want to look into

$ du --max-depth=1 -h

I know this thread is about du but you might also want to have a look at graphical applications, which can be more efficient at showing information like this, such as baobab or k4dirstat.

Francisco
  • 188
1

Maybe I'm missing what you're asking for, but I think

du -sh *

does exactly what you want. The * is important here. With -s, you get a summary for each directory on the command line, so with no explicit dir (implicit .) you would just get a summary for everything, not the for each of the dirs under the top-level individually.