7

From the shell, I need to list the size of all child directories. I am currently using:

du -hs * | sort -hr 

However, this only gets one level down and does not traverse the directory tree.

kvantour
  • 25,269
  • 4
  • 47
  • 72
Eli Reiman
  • 164
  • 2
  • 11
  • 2
    `du` displays the size of all directories in the tree by default. The `-s` option stops it doing that. What's wrong with `du -h . | sort -hr`? – pjh Jan 04 '19 at 15:23
  • 1
    [How to recursively find the amount stored in directory?](https://unix.stackexchange.com/q/67806/56041), [Display each sub-directory size in a list format using one line command in Bash?](https://superuser.com/q/554319/173513), [Using ls to list directories and their total sizes](https://stackoverflow.com/q/1019116/608639), [How to get the summarized sizes of directories and their subdirectories?](https://superuser.com/q/162749/173513), [How can I list out the size of each file and directory (recursively) and sort by size decendingly in Bash?](https://stackoverflow.com/q/7463554/608639), etc. – jww Jan 05 '19 at 00:51

5 Answers5

13

The simplest is:

du -h --max-depth=1 parent

This will show all sizes of the children of parent If you also want the grandchildren, you can do

du -h --max-depth=2 parent

If you want the whole family

du -h parent

All these will just summarize the total directory size of each subdirectory up to a given level (except the last, it will give for all)

If you don't want the content of the subdirectories, add the -S flag.

kvantour
  • 25,269
  • 4
  • 47
  • 72
1

You can use **/*

shopt -s globstar
du -hs **/* | sort -hr
shopt -u globstar
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
1

If sorting you will likely want a consistent output instead of having some in GB, some in MB, some in KB.... And if I read the OP correctly, it isn't getting the directory tree, and that's a problem, right?

It isn't listing subdirs because of the -s. Take that out (again, sorry if I'm misreading.)

du -b | sort -n

This lists all sizes in bytes, so you only need -n for sort.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
0

Wildcards are nice, but they'll also list FILES and not just directories. The below will return the sizes of all directories and only the directories:

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

UtahJarhead
  • 2,091
  • 1
  • 14
  • 21
0

The maxdepth feature should solve your issue, but for some strange reason, it seems not to work:

find ./ -maxdepth 1 -type d -exec du -k {} \;

Maybe somebody can explain why the maxdepth is not taken into account?

Dominique
  • 16,450
  • 15
  • 56
  • 112