Output folder sizes as JSON

Hello,

I've got all my Music/Movies/series sorted in a folder and I want to build a little page wich shows me the distribution and their sizes. Therefore I need some script which outputs me some JSON for further usage.

Folder structure looks like this:

├── User1
│   ├── Music
│   ├── Series
│   └── Movies
└── User2
    ├── Music
    ├── Series
    └── Movies

And I want something like this:

[{"type":"directory","name": ".","contents":[
    {"type":"directory","name":"User01","size":16384,"contents":[
      {"type":"directory","name":"Musik","size":4096,"contents":[]},
      {"type":"directory","name":"Serien","size":4096,"contents":[]},
      {"type":"directory","name":"Videos","size":4096,"contents":[]}
    ]},
    {"type":"directory","name":"User02","size":217088,"contents":[
      {"type":"directory","name":"Musik","size":200704,"contents":[
        {"type":"directory","name":"Artist01","size":4096,"contents":[]},
        {"type":"directory","name":"Artist02","size":4096,"contents":[]}      
    ]},
    {"type":"directory","name":"Serien","size":8192,"contents":[
        {"type":"directory","name":"series01","size":4096,"contents":[]}
      ]},
      {"type":"directory","name":"Videos","size":4096,"contents":[]}
    ]},
  ]},
  {"type":"report","size":237568,"directories":11}
] 

I tried many commans like

tree -d -L 3 -J --du

which outputs the above JSON but it just outputs the size of the folder itself, not the size with it's contents all together.

I hope you can understand what my problem is and someone can help me

Posted Jul 8, 2018, 3:52 PM UTC

22
Jakethe___•

The --du option per the tree man page says:

For each directory report its size as the accumulation of sizes

of all its files and sub-directories (and their files, and so

on). The total amount of used space is also given in the final

report (like the 'du -c' command.) This option requires tree to

read the entire directory tree before emitting it, see BUGS AND

NOTES below. Implies -s.

That should give you what you want. Ensure that whoever you are running the command as has permissions for the whatever folder you are running this against (i.e. User1 probably doesn't have permissions to User2's folders, so will need to run the command with escalated privilege)

1
K4limOP•

When I run tree -JL 3 --du it says. that my music folder is only 20GB but it should be around 50GB. And my artist folders are alls 4096 bytes wich os obv wrong because they have albums/tracks in it.

Edit: I didn't finish reading you answer.

This option requires tree to read the entire directory tree before emitting it

I guess I'll have to take it that ot can't stop after the third directory-level?

1