6

I want to get the size of all directories within a specific directory. I was thinking something like

find . -type d -exec du -sh {} \;

But that returns all directories recursively. How can I limit the depth?

Steve Robbins
  • 1,932
  • 5
  • 23
  • 26

6 Answers6

7

Add -maxdepth 1 to your find parameters.

platforms
  • 1,118
  • 10
  • 23
6

Why use find at all and not simply glob for directories?

du -shc */
Dennis Kaarsemaker
  • 19,277
  • 2
  • 44
  • 70
  • Well, the find option is more cross-platform, with the same command working on GNU/Linux and BSD/MacOSX systems (provided that `-maxdepth 1` is specified before the `-type d` argument). The OSX version of `du` will give a syntax error with `du -shc */`. – platforms Nov 29 '12 at 07:49
  • 1
    Fair point, but do note that the poster used linux as a tag, so I was assuming linux in my answer. – Dennis Kaarsemaker Nov 29 '12 at 07:51
  • This one is fine if you don't have hidden directories to include. – Christophe Drevet Nov 30 '12 at 09:08
  • Easily adapted to hidden directories: `du -shc */ .??*/` (The ?? is to exclude . and ..) – Dennis Kaarsemaker Nov 30 '12 at 10:17
  • This actually IS best on Linux, because it allows you to ignore the current working directory, only listing the subdirectories (what the OP specified). Find includes the `.` directory. It also gives you the choice to see the sum total or not. – platforms Dec 03 '12 at 19:38
  • Well, with -mindepth and -maxdepth find can exclude . too :) – Dennis Kaarsemaker Dec 03 '12 at 20:04
2

This one should do the job efficiently :

du -hc --max-depth=1

One big difference I think of is that, when encountering hardlinked files, they will be counted only once. In a find loop, they will be counted once per base directory. [Is it correct english?]

Christophe Drevet
  • 2,012
  • 2
  • 18
  • 26
1

You can use the -maxdepth option.

EEAA
  • 109,363
  • 18
  • 175
  • 245
1

I'm using this one,

ls | xargs du -sh 

basically there are many ways to skin a cat :)

quanta
  • 51,413
  • 19
  • 159
  • 217
Danie
  • 1,360
  • 10
  • 12
1

You can also do this:

du -ah .

What it does is to take the subdirectories of the current directory, find its individual's size and in the end, it prints the total size.