How can I get CPU count and total RAM from the OS X command line?

scorp@antani-mac:~$ hwprefs cpu_count
scorp@antani-mac:~$ hwprefs memory_size
4.00 GB
  • 15
    hwprefs doesn't seem to be part of the default OS X install (I think it's in Xcode, or maybe the CHUD tools). – Gordon Davisson Feb 16 '10 at 4:50
  • 11
    hwprefs: command not found on Mac Os x Lion – aleroot Nov 6 '11 at 10:20
  • 2
    not on Sierra either – JDS Nov 30 '16 at 20:11
  • 2
    Confirmed, not in Sierra. – Trevor Sullivan Jul 6 '17 at 20:36
  • and not on High Sierra too – Lukas Liesis Apr 7 '18 at 8:37

You can get this from the system_profiler tool:

system_profiler SPHardwareDataType | grep "  Memory:"
system_profiler SPHardwareDataType | grep Cores:
system_profiler SPHardwareDataType | grep Processors:

or, if you want to go low-level, use sysctl:

sysctl hw.memsize
sysctl hw.ncpu

Or to capture the values in a script (credit: @bleater):

mem_size=$(sysctl -n hw.memsize)
cpus_virtual=$(sysctl -n hw.ncpu)

btw, there are a bunch of other interesting things you can get from sysctl. Try:

sysctl -a | grep cpu

to see a few of them

  • +1: The sysctl method works on Mountain Lion. – Warren Young Feb 28 '13 at 16:44
  • 3
    To capture the output of sysctl in a script, use the -n option, e.g. sh/bash script: CPUS_VIRTUAL=`sysctl -n hw.ncpu` – bleater Oct 8 '13 at 2:51
  • +1: systemctl works best on 10.11.x – Danijel-James W Apr 20 '16 at 5:16
  • Thank you. By the way, output of system_profiler SPHardwareDataType is human-readable, so I would suggest against piping it to grep. – Franklin Yu May 13 '19 at 16:11
  • 4
    Fun fact: sysctl is available from the terminal in recovery mode, while many other tools are not (a coworker needed to know how much ram was in a laptop that is locked with file vault) – kbyrd Sep 16 '19 at 15:27

The following works in OS X Lion:

$ /usr/sbin/system_profiler SPHardwareDataType


    Hardware Overview:

      Model Name: iMac
      Model Identifier: iMac7,1
      Processor Name: Intel Core 2 Duo
      Processor Speed: 2.4 GHz
      Number of Processors: 1
      Total Number of Cores: 2
      L2 Cache: 4 MB
      Memory: 4 GB
      Bus Speed: 800 MHz
  • This was the most useful one for me since I got everything I was looking for from one command, and works on the recovery terminal for Yosemite (10.10). – Aaron R. Nov 24 '14 at 17:01
  • Very nice! This is the answer. – atomkirk Feb 28 '17 at 14:21
  • Note that /usr/sbin/ is usually in $PATH. – Franklin Yu May 13 '19 at 17:57
  • This still works as of Catalina, and it's wicked fast, unlike some other options. – Eric Aug 13 '20 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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