How to obtain the number of CPUs/cores in Linux from the command line?

Richard picture Richard · Jun 26, 2011 · Viewed 647.8k times · Source

I have this script, but I do not know how to get the last element in the printout:

cat /proc/cpuinfo | awk '/^processor/{print $3}'

The last element should be the number of CPUs, minus 1.

Answer

unbeli picture unbeli · Jun 26, 2011
grep -c ^processor /proc/cpuinfo     

will count the number of lines starting with "processor" in /proc/cpuinfo

For systems with hyper-threading, you can use

grep ^cpu\\scores /proc/cpuinfo | uniq |  awk '{print $4}' 

which should return (for example) 8 (whereas the command above would return 16)