How to count the number of CPU cores

cpu

Of course we know cat /proc/cpuinfo |grep "cpu cores" will give an output

[root@14:47 ~]#  cat /proc/cpuinfo |grep "cpu cores"
cpu cores       : 2
cpu cores       : 2
cpu cores       : 2
cpu cores       : 2

But actually I want to get the total number of cpu cores. I want the result to be

cpu cores: 8

How can I get such a result?

Best Answer

If you are only interested in the sum, you can also use GNU awk:

cat /proc/cpuinfo |grep "cpu cores" | awk -F: '{ num+=$2 } END{ print "cpu cores", num }'

Edit: This is the correct answer for the OP's intention to sum the numbers of the last column. However the question about finding out how many cores (physical/virtual) are on the machine is given in the other answers to the question.