4

I often use

du --max-depth=n -h | sort --human-numeric-sort

Now I want to use tree, in a similar manner. I found here a partial answer

tree -sh --sort=size --du

where --du makes tree reporting the cumulative size for each directory (as du). This reports each file as well. If I want to report only directories, I should add -d to tree. But -d seems to do two things:

  1. Remove files from the report.
  2. Remove the size of files from the cumulative total computed for each dir.

Of course, I want only 1, not 2 (as du does). So

tree -sh --sort=size --du -d

would always report "small" sizes, without considering file sizes.

Can tree overcome this? Is there any alternative?

  • you can use -I 'pattern' to exclude from the report –  Jul 13 '20 at 09:34
  • Yeah, but this also changes the dir size of the directory shown by tree, tree only summarizes what it sees... – pLumo Jul 13 '20 at 09:52
  • du --max-depth=n -h | sort --human-numeric-sort is really what I want. Thank you alot. Most of other tutorials on the internet just talk about du, and using it just show vague info like /sda... which is not helpful at all. – Thang Nguyen Mar 04 '22 at 04:52

1 Answers1

2

This is definetely a shortcoming of tree. It only summarize sizes of "what it sees". If you ignore files with -d or -I or if you limit the depth, tree will report a "wrong" dir size.


If you don't need to restrict levels, this works fine:

tree --du -shaC | sed -n '/\[01;34m/p'

Wiht 01;34 being the color value for directories from LS_COLORS env variable (...di=01;34...).

If you want to restrict levels though, the deepest level will still report wrong dir size with this method.


Consider using a more advanced tool such as ncdu.

See also this very similar question at U&L SE.

pLumo
  • 26,947