Sample Header Ad - 728x90

A standard tool to convert a byte-count into human KiB MiB etc; like du, ls1

182 votes
20 answers
94713 views
Is there a *standard tool* which converts an integer count of Bytes into a human-readable count of the largest possible unit-size, while keeping the numeric value between 1.00 and 1023.99 ? I have my own bash/awk script, but I am looking for a *standard* tool, which is found on many/most distros... something more generally available, and ideally has simple command line args, and/or can accept piped input. Here are some examples of the type of output I am looking for. 1 Byt 173.00 KiB 46.57 MiB 1.84 GiB 29.23 GiB 265.72 GiB 1.63 TiB Here is the *bytes-human* script (used for the above output) awk -v pfix="$1" -v sfix="$2" 'BEGIN { split( "Byt KiB MiB GiB TiB PiB", unit ) uix = uct = length( unit ) for( i=1; i=1024 && length(s)>1) {x/=1024; s=substr(s,5)} s=substr(s,1,4) xf=(s==" B ")?"%5d ":"%8.2f" return sprintf( xf"%s\n", x, s) } {gsub(/^[0-9]+/, human($1)); print}'
Asked by Peter.O (33644 rep)
Jul 26, 2012, 03:03 PM
Last activity: Aug 25, 2022, 10:33 AM