#!/bin/bash if [ "$1" == "-b" ] ;then shift units=(b K M G T P) duargs="-xbs" minsize=${2:-$((256*1024**2))} else units=(K M G T P) duargs="-xks" minsize=${2:-$((256*1024))} fi humansize() { local _c=$1 _i=0 while [ ${#_c} -gt 3 ] ;do ((_i++)) _c=$((_c>>10)) done _c=$(( ( $1*1000 ) >> ( 10*_i ) )) printf ${2+-v} $2 "%.2f%s" ${_c:0:${#_c}-3}.${_c:${#_c}-3} ${units[_i]} } percent() { local p=000$((${1}00000/$2)) printf ${3+-v} $3 "%.2f%%" ${p:0:${#p}-3}.${p:${#p}-3} } device=$(stat -c %d "${1:-.}") printf -v sep "%16s" "" rdu() { local _dir="$1" _spc="$2" _crt _siz _str _tot _pct while read _siz _crt;do if [ "$_crt" = "total" ]; then _tot=$_siz else [ "$_tot" ] || _tot=$_siz if [ $_siz -gt $minsize ];then humansize $_siz _str percent $_siz $_tot _pct printf "%s\___ %7s%s%7s%s%s\n" \ "$_spc" $_str "$sep" $_pct "$sep" "${_crt##*/}" [ -d "$_crt" ] && [ $(stat -c %d "$_crt") -eq $device ] && rdu "$_crt" "| $_spc" fi fi done < <( find "$_dir" -mindepth 1 -maxdepth 1 -xdev \ \( -type f -o -type d \) -printf "%D;%p\n" | sed -ne "s/^${device};//p" | tr \\n \\0 | xargs -0 du ${duargs}c | sort -nr ) } rdu "${1:-.}"