Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

On OS X, I get a nice human readable system memory reading like so:

printf -v system_memory \
          "$(system_profiler SPHardwareDataType \
             | awk -F ': ' '/^ +Memory: /{print $2}')"
echo "$system_memory"

prints out the friendly:

4 GB

Although this on Linux is correct:

lshw -class memory

it outputs:

size: 4096MiB

I need to painfully parse it and try to make it into a string as nice as the one above.

Am I using the wrong command?

share|improve this question
4  
"I need to painfully parse it"... That big ugly thing for osx isn't painful? :-) –  Patrick Mar 29 '13 at 22:22
    
@Patrick: I dislike OS X when comparing it to Linux! –  Robottinosino Mar 29 '13 at 23:20

3 Answers 3

up vote 11 down vote accepted

If that's all you need, just use free:

$ free -h | gawk  '/Mem:/{print $2}'
7.8G

free returns memory info, the -h switch tells it to print in human readable format.

share|improve this answer
    
I did not know free had a (nice) -h switch. Thanks. Very interesting how free deems 4.0G (which is what I get) more human friendly than 4 GB. "Mum, I'm hungry, I'd like 1.0 apple, please." If this is the best we have on Linux, I should probably just parse the number out and replace G with GB myself.. Very rarely do we see HDDs, for example, advertised as "320G" rather than "320 GB". :( –  Robottinosino Mar 29 '13 at 23:31
4  
@Robottinosino, HDD sizes are expressed in GB (10^9 bytes), while memory is generally expressed in GiB (2^30 bytes), 4.0G gives you an idea of the precision. With 4GB, you don't know if it's exactly 4GB (or 4GiB?) or 4.4GB rounded down to 4 or 3.6G rounded up to 4. –  Stéphane Chazelas Mar 30 '13 at 0:00
    
It is, certainly, more precise to display it as so.. no doubt about that.. probably the "humans" this is intended for are still aware of the issues you raise.. (I had not thought of that). –  Robottinosino Mar 30 '13 at 0:05

On Linux,

read x memtotal x < /proc/meminfo

Would store the total mem amount in $memory in number of kiB. That's the amount of memory available to Linux, the same as reported by free.

If you want the installed RAM, you could do things like:

awk '{s+=$0};END{print s}' /sys/bus/mc*/devices/dimm*/size

To get the size in MiBs. Or

awk '{s+=$0};END{printf "%.2gG\n", s/1024}' /sys/bus/mc*/devices/dimm*/size

If you want the size in GiB.

share|improve this answer

it outputs:

size: 4096MiB

I need to painfully parse it and try to make it into a string as nice as the one above.

Am I using the wrong command?

No, but you're not using it like the author recommends, that is, run it as root.

lshw must be run as super user or it will only report partial information.

If you run it as root you will get the desired output, in human readable form, in SI units, in the largest possible unit-size. On my machine:

su -

then

lshw -businfo -C memory | awk '/System\ Memory$/{print $2}'

outputs:

8GiB
share|improve this answer

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.