Human readable and by size sorted disk usage (du) in BASH

por | 11 diciembre, 2012

function duf {
du -sk "$@" | sort -n | while read size fname; do for unit in k M G T P E Z Y; do if [ $size -lt 1024 ]; then echo -e "${size}${unit}\t${fname}"; break; fi; size=$((size/1024)); done; done
}

Just put this function into your ~/.bashrc to make it permanent.

expanded code
function duf {

// get usage in KBytes and sort
du -sk «$@» | sort -n | while read size fname;

// loops for each size
do for unit in k M G T P E Z Y;

// if size<1024 we found the correct suffix
if [ $size -lt 1024 ];

// display the size
then echo -e «${size}${unit}\t${fname}»;

// line completed
break;
fi;

// for each sizes suffix divide by 1024
size=$((size/1024));

done;
done
}

When done, you can use your new function like this: duf /*.

 

 


other version

 

 ..alias duf='du -sk * | sort -n | perl -ne '\''($s,$f)=split(m{\t});for (qw(K M G)) {if($s<1024) {printf("%.1f",$s);print "$_\t$f"; last};$s=$s/1024}'\'

Categoría: Nix