سؤال

I often work on unix boxes that don't have the -h flag for du.

I am looking for a one-liner to convert KB to human readable. Perl seemed like a good choice.
This is what I have so far.

@a=split /\s+/;
$x=$_!=0?int(log()/log(1024)):0;
@b=('K','M','G');
printf("%.3s%s\t%s\n",$_/(1024)**$x,$b[$x],$a[1]);

Run like this:

du -ks * | perl -lne '@a=split /\s+/;$x=$_!=0?int(log()/log(1024)):0;@b=('K','M','G');printf("%.3s%s\t%s\n",$_/(1024)**$x,$b[$x],$a[1]);'

It doesn't work perfectly as I haven't been able to find the correct printf format.

one-liners using perl as well as awk/sed, etc. would be the most useful.

This is what du -h looks like. Max 1 decimal. Min: 0 decimals. With Rounding.

8.0K
1.7M
4.0M
5.7M
88K

Update:

du -ks * | perl -lane '$F[0];$x=$_!=?int(log()/log(1024)):0;printf("%.3s%s\t%s\n",$_/1024**$x,qw<K M G>[$x],$F[1]);'
هل كانت مفيدة؟

المحلول

If the only modification you want to make (It's not clear what you want) is to have the number be right-aligned in a field of 3 characters, simply drop the period from the printf format. Also, rather than explicitly calling split and treating the whole of $_ as a number, I would recommend passing Perl the -a switch, which automatically splits $_ on whitespace into the array @F, and then replace the references to $_ in your code with $F[0].

Your code could thus be rewritten (using a couple more Perlisms and adding some spaces for readability) as:

du -ks * | perl -lane '$x = $F[0] != 0 && int(log($F[0])/log(1024)); printf("%3d%s\t%s\n", $F[0]/1024**$x, qw<K M G>[$x], $F[1]);'

نصائح أخرى

This uses Number::Bytes::Human from CPAN:

du -ks * |perl -MNumber::Bytes::Human=format_bytes -nle \
    '@F=split(/\s+/,$_,2); printf("%-10s%s\n", format_bytes($F[0]*1024), $F[1])'

EDIT: Without using modules:

du -ks * |perl -nle \
   '@F=split(/\s+/,$_,2); $b=$F[0]*1024; for($i=0;$b>1024;$i++){$b/=1024} $u=qw{B K M G T}[$i]; printf("%10.".($b=~/\./?1:0)."f$u  %s\n", $b, $F[1])'

your correct printf() format:

sub get_filesize_str
{
    my $file = shift;

    my $size = (stat($file))[7] || die "stat($file): $!\n";

    if ($size > 1099511627776) {   #   TiB: 1024 GiB
        return sprintf("%.2f TiB", $size / 1099511627776);
    } elsif ($size > 1073741824) { #   GiB: 1024 MiB
        return sprintf("%.2f GiB", $size / 1073741824);
    } elsif ($size > 1048576) {    #   MiB: 1024 KiB
        return sprintf("%.2f MiB", $size / 1048576);
    } elsif ($size > 1024) {       #   KiB: 1024 B
        return sprintf("%.2f KiB", $size / 1024);
    } else {                       #   bytes
        return sprintf("%.2f bytes", $size);
    }
}

that's not my code, it was taken from here

du -sk * | perl -ane '
  $i=0;
  while ($F[0]>1024) {$F[0]/=1024; $i++}; 
  printf("%d%s\t%s\n", $F[0], qw(K M G)[$i], $F[1])
'

If you want fractions on the bigger numbers:

du -sk * | perl -ane '
  $i=0;
  while ($F[0]>1024) {$F[0]/=1024; $i++;};
  $f = $i==0 ? "d" : ".2f"; 
  printf("%$f%s\t%s\n", $F[0], qw(K M G)[$i], $F[1])
'

Here is AWK function adapted from some answer on stackoverflow:

function human_readable(sum) {

hum[1024**3]="GiB";hum[1024**2]="MiB";hum[1024]="KiB"; 
    for (x=1024**3; x>=1024; x/=1024){ 
        if (sum>=x) { v = sprintf( "%.2f %s",sum/x,hum[x]); return v }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top