A simple “treesize” shell script for Linux

One of my favorite pieces of software on windows is a little app called treesize free by Jam Software. It basically gives you a simple list of how much disk space each directory is taking up. This is really useful when you are trying to work out where all the space on your 500gig disk is gone.

I am always finding myself looking for similar piece of software for linux which can be run simply from the command line, but alas none exists so i decided to create a simple shell script to do a similar job, and here it is :

#/bin/sh
du -k --max-depth=1 | sort -nr | awk '
     BEGIN {
        split("KB,MB,GB,TB", Units, ",");
     }
     {
        u = 1;
        while ($1 >= 1024) {
           $1 = $1 / 1024;
           u += 1
        }
        $1 = sprintf("%.1f %s", $1, Units[u]);
        print $0;
     }
    '

Just put this code into a file /bin/treesize and make it executable. Then any system user can get a list of directory sizes within a directory by just running treesize from any directory.

Leave a comment ?

13 Comments.

  1. Thanks so much for your script! I also like Treesize and had been looking for a Linux shell equivalent.

  2. Awesome Script… works exactly as how I needed…

    Thanks for sharing.

  3. Thanks :-) . The script is very helpful

  4. Awk! I miss awk!

  5. The post is a bit old, but anyways. :)
    Even though it’s elegant, your script is a bit overkill. du has this functionallity built in.

    du -sch ./*

  6. Thanks for the script, very useful.

    I used to use du -sm * | sort -nr but this is much quicker, particularly if I change the max-depth parameter if required.

  7. Hello !
    Thank you VERY MUCH !
    I am used to use Treesize for Windows too, and I needed the same think for my Synology NAS.
    The only change is the “du” command :
    du -k -d 1

    Thank you!

  8. Hey, thanks for the script.

    for linux you can try the “baobab”

  9. Great! This is so helpful in working on ubuntu server via console or ssh. I’m supposed to try to avoid using GUI for my class assignments, and even in my personal work (like trying to figure out what on earth is taking up my virtual hdd’s space!) I’m trying to be all command line.

  10. some newer systems:

    du -shx ./* | sort -h

    It is not the treesize that way but excludes other file systems (x) summarizes sub-dirs (s) and sorts human readable. (if available in the sort there)

  11. spooky… almost exactly a year later i need this again, please never ever let this page 404.

  12. Excellent Script i like so much.
    easy & simple

  13. +1 thanks@!

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>