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 :

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.

26 thoughts on “A simple “treesize” shell script for Linux

  1. Luke Moynihan

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

  2. Fredrik

    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 ./*

  3. Mark Peters

    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.

  4. ELO

    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!

  5. Wally

    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.

  6. R

    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)

  7. Pingback: Treesize für Linux Script | µBrain-Blog

  8. Robin

    I personally prefer ‘ncdu’ (https://dev.yorhel.nl/ncdu/scr). It’s available on most distro’s.

    Other options (copy/paste from ncdu site):
    gt5 – Quite similar to ncdu, but a different approach.
    tdu – Another small ncurses-based disk usage visualization utility.
    TreeSize – GTK, using a treeview.
    Baobab – GTK, using pie-charts, a treeview and a treemap. Comes with GNOME.
    GdMap – GTK, with a treemap display.
    Filelight – KDE, using pie-charts.
    KDirStat – KDE, with a treemap display.
    QDiskUsage – Qt, using pie-charts.
    xdiskusage – FLTK, with a treemap display.
    fsv – 3D visualization.
    Philesight – Web-based clone of Filelight.

  9. Allann

    Thanks !
    But I find this command easier to use:
    for X in $(du -s * | sort -nr | cut -f 2); do du -hs $X ; done


Leave a Reply

Your email address will not be published. Required fields are marked *