Operating System - Linux
1752510 Members
4647 Online
108788 Solutions
New Discussion юеВ

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

 
SOLVED
Go to solution
Sammy_2
Super Advisor

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 7
James R. Ferguson
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...
James R. Ferguson
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...
Sammy_2
Super Advisor

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.
James R. Ferguson
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 :-)

NO POINTS PLEASE!

...JRF...
James R. Ferguson
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...
Hein van den Heuvel
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:
http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=542488

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.
Bill Hassell
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)
(requires read permission for mountpoint)

File(s) or dirpath(s) may be specified to limit
the output of bdfmegs as in:

bdfmegs -vlg /usr/contrib/bin /var/tmp

If bdfmegs is run as bdfgigs (ie, a link), then -g is default.
(bdfmegs ver 4.7)


Bill Hassell, sysadmin