How to use tree or du command to get directory content sizes?

Viewed 170

I'm using the tree command to get a list of directories:

tree -ifd --du

This, however, seems to be only displaying the size of the directory itself and not the total for its contents.

How would I modify this command to show the total space used by each directory WITH its contents?

edit: This du command seems to come close to doing what I want but has a sorting issue:

du -h ./* | sort -k 2

The above produces a list like this:

62G     ./Directory
78G     ./Directory 3-14-18
36G     ./Directory 3-14-18/#0986
42G     ./Directory 3-14-18/#0987
38G     ./Directory 3-21-18
38G     ./Directory 3-21-18/#1229
31G     ./Directory/#7852
31G     ./Directory/#7853

Note that, in the above, the first directory, the one with no date as part of its name, is at the top but it's subs are far below. I find that, if I add even a phony date to the end, something like 0-00-00, it sorts correctly:

62G     ./Directory 0-00-00
31G     ./Directory 0-00-00/#7852
31G     ./Directory 0-00-00/#7853
78G     ./Directory 3-14-18
36G     ./Directory 3-14-18/#0986
42G     ./Directory 3-14-18/#0987
38G     ./Directory 3-21-18
38G     ./Directory 3-21-18/#1229

Additional question here would be:

How do I get the sorting to work WITHOUT having to add phony dates to everyplace I find this issue?

end edit

edit2: Per the question from AndrewF below, I'll explain that I have a lot of archived media on hard drives. I can't run them all in my system at the same time so I thought it might be a good idea to have printed lists of what's on each drive.

To that end, I'm using tree to list the entire contents of each drive. I'm trying to use du to make a much shorter list of only directories as well.

As I get these drives organized, I expect to have to do a lot of manual digging and being able to view multiple lists should give me an ability to find redundancies that can be eliminated and deleted even though I can't have physical access to all of the drives at the same time. In other words, while I can't mount all of the drives, I can view all of the lists and use them to find redundancies, then mount the drive with the redundancy, and delete the extraneous directories. Having things sorted out of order is just going to make mistakes more likely.

end edit2

thanks, babag

1 Answers

I wouldn't use tree because I don't have it on all the platforms I use. But I imagine that find . -type d is well-suited for your use case, so:

find . -type d | xargs du -sh

Depending on the directory you choose, you may get a lot of errors like "permission denied" to stderr, and if these are printing to your terminal but you want them to stop:

find . -type d 2>/dev/null | xargs du -sh 2>/dev/null

Those examples will run du once per entry returned by the find.

Note that none of these are going to be efficient -- you're going to be analyzing the same files/directories many times to get those rolled-up summary sizes. Better solutions would involve depth-first traversal and memoization, and I don't know any available tools that do it.

Edit: I don't understand your goal with the sort. If the find does not list out all entries in the order you want, you'll need to provide extra details about what exact kind of sorting you want to do. It might be best to put that sorting problem into a new more specific question.

Related