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,204
  • 2
  • 57
  • 87
  • Hi, pLumo. Nice idea. But I think the OP asks for something like this: `tree --du -shaC | sed -r '/\[01;34m/!{s/\[.*\]//}'`. – pa4080 Jul 13 '20 at 10:09
  • I didn't find a way to have `ncdu` output a tree, with attached cumulative spaces, so I can e.g. redirect to file and inspect later on. I can interactively navigate the tree, but that is now what I was looking for. – sancho.s ReinstateMonicaCellio Jul 13 '20 at 10:42
  • You can use `-o -` to output a json that you can then filter with `jq` or `python ` ... But yes you are right, but there might not be an optimal solution – pLumo Jul 13 '20 at 10:44
  • @pa4080 - Your solution seems to work reasonably well. Have to check the deatils. do you know of any way to sort by size? `--sort=size` is giving me strange results. Plus, `sed` filtering seems to even screw that strange output. – sancho.s ReinstateMonicaCellio Jul 13 '20 at 10:45
  • Hi, @sancho.sReinstateMonicaCellio, unfortunately I do not have an idea how to do sort by size in a easy way in this context. Probably this topic cold be interesting for you: [Recursive bash script to collect information about each file in a directory structure](https://askubuntu.com/q/968887/566421) – pa4080 Jul 13 '20 at 12:41
  • @pa4080 - Ok, will check it and get back to you if needed. Thanks for the pointer. – sancho.s ReinstateMonicaCellio Jul 13 '20 at 20:40