I’ve written a simple shell script that finds large files, mostly to save myself some typing. The work is being done with:
find $dir -type f -size +"$size"M -printf '%s %p\n' | sort -rn
I’d like to turn the byte output into a human readable format. I found ways online on how to manually do this, e.g.,
find $dir -type f -size +"$size"M -printf '%s %p\n' | sort -rn |
awk '{ hum[1024**4]="TB"; hum[1024**3]="GB"; hum[1024**2]="MB"; hum[1024]="KB"; hum[0]="B";
for (x=1024**4; x>=1024; x/=1024){
if ($1>=x) { printf "%7.2f %s\t%s\n",$1/x,hum[x],$2;break }
}}'
But this seems messy. I was wondering: is there was a standard way to convert bytes into a human-readable form?
Of course, any alternate methods of producing the below output, given a directory and min-size as input, are also welcome:
1.25 GB /foo/barf
598.80 MB /foo/bar/bazf
500.58 MB /bar/bazf
421.70 MB /bar/baz/bamf
...
Note: This must work on both 2.4 and 2.6, and the output should be sorted.
for instance 🙂 or
(with a little inspiration borrowed from olibre).