tree, get me directory size!

Getting an understanding of how your drive is filled up is very important; especially on systems with no GUI. Let's see how the tree is helpful.

tree, get me directory size!

I was working with the Droplets and Volumes on DigitalOcean and down the line, I had a question: How much space my Volume is using?

And based on the above question, I would choose to expand or reduce the size of the Volume.

The same can go for Storage on other providers or local machines.

You NEED to know the usage and plan accordingly.

It would be fast and easy to do so in GUI of OS but if you are connected via terminal or GUI is not enabled, it becomes a constant need to be mentally aware of the size.


I came across a few options, like: diskusage (or du on the command line)

Let's see how du works. Assuming that I am in my code directory and wanna analyze assets inside casper (the ghost theme)

du casper/assets

But that's not very readable; fortunately -h is here to show the size in human-readable format:

du -h casper/assets

But above outputs are still not that human-readable.

  • All the paths matching the said pattern will be shown
  • Files are missing

Solution? tree


tree utility

The tree utility is a good alternative to obtaining the size of the directories.

But tree won't give the size results to you right away; you would have to ask for it.

Let's try to run it on the above example; assets in casper

To get the size, we will need to add the -s flag; let's see what it shows:

Still not good, we can't make sense of the size displayed; it is in bytes

We can use the same flag -h as of du to get the size human-readable:

If you want just the directories and no files in the output; we can add -d flag which will show the following output on our assets directory example:


Installation

Now it might be possible that you don't have it installed on your computer.

To install it, you can run the following commands depending on the OS:

macOS

brew install tree

Linux (apt)

sudo apt install tree

Linux (yum)

sudo yum install tree

WSL (Windows Subsystem for Linux)

Depending on what Linux subsystem you install, you can use one of the above Linux commands


What's next?

More things tree can offer

The tree command is capable of more things such as

  • coloured output
  • permissions in the tree output
  • inode
  • filtering
  • etc.

Check out the man page for tree here: https://linux.die.net/man/1/tree or on Command Line:

man tree

Use with other utilities

As it is a completely synchronous utility, you can pipe its output to any other CLI utility or tool.

For example,  you can export a beautiful snapshot of tree output with carbon. As there is a CLI utility of carbon named carbon-now-cli with preset

tree -h assets | carbon-now -p presentation

Conclusion

Have you used the tree command recently?

What other cool flags you have used it with?

Let me know through comments 💬 or on Twitter at @heypankaj_ and/or @time2hack

If you find this article helpful, please share it with others 🗣

Subscribe to the blog to receive new posts right to your inbox.

Hey There! You have made it this far.

Would you like to subscribe via email?