#!/bin/bash function helpexit() { echo "Display file/folder sizes like 'du', but faster and actual byte sizes" echo "Returns only the size as number" echo " -h Human readable units" echo " --help This help" exit } function filesize { # Return a human readable size from integer of bytes # Usage: filesize 10000 [ "$1" = "0" ] && { echo "0 B" return 0 } awk 'BEGIN{ x = '$1' split("B KB MB GB TB PB EB ZB",type) for(i=7;y < 1;i--) y = x / (2^(10*i)) str=int(y*10)/10 " " type[i+2] if (x==0) { str = "0 B" } print str }' || return $? } [[ -z "$1" ]] && what="." [[ "$1" = "-h" ]] && { HUMAN=1; shift 1; } [[ "$1" = "--help" ]] && helpexit SIZE=$( find $what "$@" -type f -printf %s"\n" | awk '{ sum += $1 } END { print int(sum)+0 }' ) [[ "$HUMAN" -eq 1 ]] && { filesize "$SIZE" exit } echo $SIZE