Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm working on a little util tool written in bash that gives me some information about a game server on Linux. For that reason I need a possibility to display the size of the file contents.

I'm doing that right now by using this:

du -Lshc *

It works almost perfect! The only flaw is that it displays stuff like this:

21G     backups
22G     server
8.0K    start.sh
151M    world.zip
43G     total

This is fine but I want more digits. Meaning it should look like that:

21.587G     backups
22.124G     server
8.089K      start.sh
151.198M    world.zip
43.436G     total

Is there a possiblility to do that?

I wouldn't mind using a complex command since I use it in a .sh file so I won't type it by hand every time.

share|improve this question

1 Answer 1

up vote 3 down vote accepted
du -Lsbc * | awk '
    function hr(bytes) {
        hum[1024**4]="TiB";
        hum[1024**3]="GiB";
        hum[1024**2]="MiB";
        hum[1024]="kiB";
        for (x = 1024**4; x >= 1024; x /= 1024) {
            if (bytes >= x) {
                return sprintf("%8.3f %s", bytes/x, hum[x]);
            }
        }
        return sprintf("%4d     B", bytes);
    }

    {
        print hr($1) "\t" $2
    }
'

awk-function based on this.

One could probably make the output look a bit nicer by piping it through column or left-padding it with spaces.

Edit: Added the left-padding.

Also, to sort the list: du -Lsbc * | sort -n | awk and then the awk-script.

share|improve this answer
    
This is almost perfect. It only has a little issue if the file size is 0. Then no number is displayed. Another thing is that there are decimal places if the unit is B (Meaning it looks like 108.000B. Which is some kind of akward.) But thank you anyways! –  BrainStone Aug 18 '13 at 13:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.