Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Languages and Scripting
cancel
Showing results for
Did you mean:

SOLVED
Go to solution

## bdf | awk '{ sum += \$2; } END { print sum; }' for total space in Megabytes ??

bdf | awk '{ sum += \$2; } END { print sum; }'
gives me 5.03521e+07

Not sure what that means. How can I get output in Megbytes ?
good judgement comes from experience and experience comes from bad judgement.
7 REPLIES
Acclaimed Contributor
Solution

## Re: bdf | awk '{ sum += \$2; } END { print sum; }' for total space in Megabytes ??

Hi Sammy:

This is simply scientific notation. This is 5.03521 times 10 raised to the 7th power:

50,352,100

To obtain megabytes (ideally) divide by 1024/1024 or:

# bdf | awk '{ sum += \$2; } END { print sum/1024/1024; }'

Regards!

...JRF...
Acclaimed Contributor

## Re: bdf | awk '{ sum += \$2; } END { print sum; }' for total space in Megabytes ??

Hi (again) Sammy:

I would also direct you to this article and its links which show that the terms 'mega', 'giga', etc. become imprecise as the magnitude of numbers increase. A acccurate terminology is 'mebi'byte, here:

http://en.wikipedia.org/wiki/Mebibyte

Regards!

...JRF...

## Re: bdf | awk '{ sum += \$2; } END { print sum; }' for total space in Megabytes ??

JRF, Thanks and it got what i needed. But, is it a typo or 1024/1024 in your solution would give you in GB as oppose to MB
good judgement comes from experience and experience comes from bad judgement.
Acclaimed Contributor

## Re: bdf | awk '{ sum += \$2; } END { print sum; }' for total space in Megabytes ??

Hi Sammy:

Ooops, sorry, I'm thinking giga/gibi bytes :-)

...JRF...
Acclaimed Contributor

## Re: bdf | awk '{ sum += \$2; } END { print sum; }' for total space in Megabytes ??

Sammy:

Now that I have had coffee and lunch, after a long night, I would suggest you amend your formatting to round your values, too. The 'printf' function accomodates this. Too, I would also instruct 'awk' to skip the first record (the header line from 'bdf') as a cleaner implementation, although in this case there is no second field in the header.

# bdf | awk 'NR>1 {sum+=\$2};END {printf "%5.0f MB\n",sum/1024}'

No points are necessary for what I ahould have added the first time!

Regards!

...JRF...
Honored Contributor

## Re: bdf | awk '{ sum += \$2; } END { print sum; }' for total space in Megabytes ??

Sammy,

If BDF does the job for you, then more power to you! However, several of us run into issues with it due to the bonus line-wrapping it does for long names.

See for example:

So you may want to build a serious script with DF looking for field \$4 or "dk -k" looking for field \$5 on lines with "total"

Also... you want to count NFS mounts?

Consider using bdf | awk '/^.dev/{ sum...
# The "^" anchors te search to the begin of line.
# The "." is a cheat place holder for /

fwiw,
Hein.
Highlighted
Honored Contributor

## Re: bdf | awk '{ sum += \$2; } END { print sum; }' for total space in Megabytes ??

This script will probably give you everything you need. You can even select the mountpoints for a summary, like this: bdfmegs -s /dev/vg00/lvol* Here is the usage:

Usage: bdfmegs [ -glPpqstvu ] [ dir(s) or file(s) ]
where:
-g = gigabytes rather than megabytes
-l = local only (not NFS)
-p ## = highlight pct -ge ##
-P ## = same as -p but show only pct -ge ##
-q = suppress the header line and no char enhancements
-s = summary of total, used and available
-t xxxx = specifc filesystem (hfs vxfs nfs cdfs cifs autofs DevFS)
-u = usage details (return code = 0, usage text to stdout)
-v = verbose (shows FSversion, largefiles flag)