Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6729017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:14:19+00:00 2026-05-26T10:14:19+00:00

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

  • 0

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]);'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:14:20+00:00Added an answer on May 26, 2026 at 10:14 am

    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]);'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't do database work that often so this is totally unfamiliar territory for
I often have to work with fragile legacy websites that break in unexpected ways
Often functions that work with data.frames have the ability to let the user provide
I work for a site that often get's attacked by bot networks. We have
I often work with sales and marketing types that cannot figure out how to
Often I work with heroku, and near the end of a project I have
I often work with text files which have a variable amount of whitespaces as
At work, I often have to deal with 3rd party libraries, which make heavy
I work for a rather busy internet site that is often gets very large
I work with the rather finnicky Oracle Business Intelligence software and we often have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.