5

I can't solve what I want using the tree, du, or df commands.

I'm using:

  • du version 9.6
  • tree version 2.2.1

What I want:

For the current directory, list all directories recursively to a depth of 3, showing their size and ordering them by size. For clarity, each level of subdirectory ought to be indented.

Using du -h -d 3 does not provide a tree view.

Now, tree seems to be the command to use, but I do not understand the output, so I made a test directory and here are the outputs. In the following, I do not understand the answer with

  • tree --du -h -L 2 for the directory size.
  • tree --du -h -L 3 for the directory size of immo2 as is 16M and not 4.0K, as well as immo and root size 102M and not 86M.

Remark: tree --du -h -L 2 and tree -du -h -L 2 do not provide the same output.

Thanks.

$ tree --du -h -L 3
[ 86M]  .
└── [ 86M]  sub1
    ├── [3.6M]  immo
    │   ├── [ 670]  00Tree.html
    │   ├── [416K]  2013-08-27_120009.jpg
    │   ├── [3.2M]  2014-04-09_093940.jpg
    │   └── [4.0K]  immo_2
    └── [ 82M]  ogg
        ├── [ 141]  00Tree.html
        └── [ 82M]  2020-06-21.ogg

  86M used in 5 directories, 5 files
$
$ tree --du -h -L 2
[4.0K]  .
└── [4.0K]  sub1
    ├── [4.0K]  immo
    └── [   0]  ogg

 4.0K used in 4 directories, 0 files
$
$ du -h -d 3
16M ./sub1/immo/immo_2
20M ./sub1/immo
83M ./sub1/ogg
102M    ./sub1
102M    .
$

P.S. In the meantime,

1/ filed a bug report for tree cmd.

2/ I found an awesome app: https://apps.kde.org/fr/filelight/

5
  • I'm assuming that tree --du -L 2 only shows the sizes of things that it shows, not things below the given depth.
    – Kusalananda
    Commented Apr 11 at 12:50
  • Downvoting worked-out answers that probably solve questions without explaining why isn't cool at all and it is mostly done by people with a strange attitude. I delete my posting and probably never answer any other question here again.
    – jottbe
    Commented Apr 12 at 9:38
  • @jottbe Your answer got two downvotes and two comments detailing what needed to be fixed. Both comments addressed the tree structure output (which was lacking in your answer), and one addressed the inefficiency of calculating directory sizes recursively for each level of the directory hierarchy.
    – Kusalananda
    Commented Apr 12 at 12:57
  • Dear @Kusalananda: the comments were added only after I complained, before it was just downvoted. Probably to list it down This was my first answer here. If you think people who are treated this way keep spending their time elaborating on other questions, you must be dreaming.
    – jottbe
    Commented Apr 14 at 2:15
  • @jottbe If you want to continue discussing the possible reasons why your answer was downvoted or how this site works in general, you are welcome to post about it on our Meta site, unix.meta.stackexchange.com
    – Kusalananda
    Commented Apr 14 at 2:40

2 Answers 2

1

Since tree --du -L 3 only shows the sizes of things down to the given depth but ignores everything below, giving incorrect total sizes for directories, allow tree to see to the bottom of the directory tree and then cut off the branches that are longer than what you're interested in.

The output from tree --du will contain a [ in column 1 for the top level, in column 5 for level 1, and in column 4*n + 1 for level n. To prune branches at level 3 or deeper, corresponding to using -L 2, we therefore remove all lines whose first [ is found in column 13 or later.

tree --du -h --sort=size . | sed -E '/^[^[]{12,}\[/d'
tree --du -h --sort=size . | grep -v -E '^[^[]{12,}\['

Change 12 to 16 for the equivalent of -L 3.

Or, easier on the eyes, only print the lines that contain a [ within the first 12 columns (does not print the summary line):

tree --du -h --sort=size . | sed -n -E '/^.{0,11}\[/p'
tree --du -h --sort=size . | grep -E '^.{0,11}\['

Change 11 to 15 for the equivalent of -L 3.


Example:

Listing the full structure:

$ tree --du -h --sort=size .
[164M]  .
├── [106M]  book
│   └── [106M]  c
│       └── [106M]  d
│           └── [106M]  file
└── [ 59M]  bird
    └── [ 59M]  c2
        └── [ 59M]  file2

 164M used in 6 directories, 2 files

Using -L 2 means not reaching down to the actual files:

$ tree --du -h --sort=size -L 2 .
[ 20K]  .
├── [8.0K]  bird
│   └── [4.0K]  c2
└── [8.0K]  book
    └── [4.0K]  c

  20K used in 5 directories, 0 files

So instead, we list the full structure and prune the deeper branches:

$ tree --du -h --sort=size . | grep -v -E '^[^[]{12,}\['
[164M]  .
├── [106M]  book
│   └── [106M]  c
└── [ 59M]  bird
    └── [ 59M]  c2

 164M used in 6 directories, 2 files

You also noted that tree --du and tree -du produce different outputs. This is because tree -du is the same as tree -d -u, which causes the utility to only report directories (-d) and to print the username or UID of the users owning them (-u).

1
  • 1
    Worth noting that GNU du deduplicates hard links by default and tree skips hidden files by default, so du and tree may still give you different results. Commented Apr 13 at 17:40
0

It seems that tree --du doesn't recurse but just adds the sizes of the subdirs and if there's a -L argument saying it shouldn't descend into a subdirectory, it's size is never measured, and thus can't be added to the size of the directories above. Here tree --du also outputs a trailing line, trying to calculate the total usage, but that is (also) total bogus, as can be seen here:

grove@wai> tree --du -h -L 6
[100M]  .
└── [100M]  a
    └── [100M]  b
        └── [100M]  c
            └── [100M]  d
                └── [100M]  e
                    └── [100M]  f

 600M used in 6 directories, 1 file

(in case it's not clear from the main output, there's one file of 100M size in the f directory)

I would consider both behaviours bugs, and if I used tree myself would file a bug report, now I'll leave it to you (or someone who cares).

2
  • On Alpine Linux, with tree v2.2.1, I don't see the adding up bug.
    – Kusalananda
    Commented Apr 11 at 13:41
  • Okay, then they apparent fixed that, my Debian Bookworm has v2.1.0 (v2.2.1 is already in what will become Trixie - hopefully later this year). Then there's obviously no reason to file a bug about that - being responsible enough to check if the bug still exists in newer versions was part of what I didn't bother to do because I don't use tree. Commented Apr 11 at 15:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.