Sorted and aggregated disk usage script

I've been asked for this script many times lately, so I'll publish it. It lists the disk usage of subdirectories in a given directory in descending order, right justified and suffixed with kilobytes or megabytes appropriately.

#!/bin/sh

display() {
 local DIR=$1
 du -sk $DIR/..?* $DIR/.[!.]* $DIR/* 2>/dev/null |
  sort -n |
  awk '
   {
    size = $1;
    sum += size;
    name = $2;
    if ($1 < 1024){
     printf("%9ik %s\n",size,name);
    } else {
     printf("%9.3fM %s\n",size/1024,name);
    }
   }
   END{
    print sum " kiB"
   }
  '
}

main() {
 local DIR=$1
 if [ "$DIR" = "" ]
 then
  display $HOME
  quota
 else
  display $DIR
 fi
}

main "$@"

Comments

Popular posts from this blog

Tftp secret of TL-WR740N uncovered

Hidden TFTP of TP-Link routers

Haskell for embedded: C output, compilers, monads, Timber