Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convering du output to Human Readable Form 4

Status
Not open for further replies.

Trikk66

MIS
Jan 28, 2005
6
US
I know this is a simple thing...but I have stared at it for so long its driving me NUTS!

How can one convert the SCO Unix output of du to a Human Readable (i.e. give the size in Megs) for output?

example:

du outputs a directory size of 261648

converting that to a simple human understandable number of Megs used would be done how?

I have my own ideas...but need to check against the Guru's as I have looked at this so long I am not sure I am correct anymore... ;-)
 
You can add the -k option to the du command to have the output displayed in K-Bytes.

I know you asked for MBytes. You can either do the mental math, or write a one-liner that will divide by 1000.
 
The truly sad aprt of this is I can't get the one-liner to work!

((X=$U/1000)) keeps erroring out

$U is the output of the du -k command on the given directory...

I am using ksh by the way...

Like I said...I KNOW it's simple...I am just having a Senior Moment!

I appreciate your input!

 
The truly sad part of this is I can't get the one-liner to work!

((X=$U/1000)) keeps erroring out

$U is the output of the du -k command on the given directory...

I am using ksh by the way...

Like I said...I KNOW it's simple...I am just having a Senior Moment!

I am writing this for a group of people who are totally computer illiterate (and this issue is giving me problems!)

I appreciate your input!

 
#!/usr/dt/bin/dtksh

float u=1334.24230000
float x=$((u/1000))
 
Bless you kHz!

The darned $ !!!!!

I never see those on my paycheck...so its obvious why I would forget that darned thing!
 
I made a complete small convert-number-to-bytewhatever, for any number seen by spaces
eh im lacking so english right now..

mconv.awk - as mathconv, i shall name it bconv or something.
Code:
#!/usr/bin/awk -f

BEGIN { split("5 P 4 T 3 G 2 M 1 k", Size); base = 1024
        #print rsize(3)
}

{
    for (i = 1; i <= NF; i++) {
            if ($i !~ /^[0-9]+$/) continue
            $i = mconv($i)
    } print
}

function mconv(num      ,n) {
    while (Size[++n]) {
            if (num / rsize(Size[n]) > .1)
                    return sprintf("%01.3g%s",
                    num / rsize(Size[n]), Size[n + 1])
            n++
    }
}

# if ** would work with non gawk, there wouldnt be that function
function rsize(times    ,res) {
        res = 1
        do res *= base; while (--times)
        return res
}

has the disadvantage of breaking the original spacing/tabbing of the output... if your SCO toolchain contains 'column', that would make df output again viewable.
also df needs to be told to use 1b blocks, using 512 or 1k requires tuning it, the latter just by shifting the numbers in the Size array.

df -B 1 | awk -f mconv.awk | column -t

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
This is more of what I had in mind (simple one-liner):

$ du -s ADATA
82002 ADATA

$ ./doit2 ADATA
41.001 Megabytes

$ cat doit2
du -ks $1|awk '{ print $1/1000 " Megabytes"'}
 
Motoslide...

You have just saved what is left of my sanity!

I have never been very good with awk...but this is FANTASTIC

 
I caught the tic placement...and I aint gonna gripe...;-)

Like I said...ever stare at something so long the simple became complex?

Thanks again Moto

And VLad...better hurry back to your seat...recess is OVER
 
Vlad:
The default output from du is displayed in 512-byte blocks. The -k part of the du changes the output to be KBytes (at least in SCO 5.0.x).

Futurelet:
You're correct, of course, about dividing by 1024 instead of 1000. But, that argument has already been beat to death within these forums. I stand corrected.
 
ok, but I get the same output with or without '-k' under Solaris - stange, I might be missing something.

I was just wondering how you were getting 41.001 by 82002/1000.

I guess I'll stick around the grownup's sandbox for now.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
du is weird in many aspects.. -B1 is supposed to show the bytes, but its just multiplied by the small -k integer, where -b (the alias for -B1) gives the exact amount.
Some few other examples where du just _wont_ give the true size vs ls or find are seen.

The not-too-well-known IEC standard about names and symbols for prefixes for binary multiples

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top