Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

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

I have this script, yet I don't have a clue how to get the last elements in the printout:
cat /proc/cpuinfo | awk '/^processor/{print $3}'


The last elements ought to be the quantity of CPUs, minus 1.
by

2 Answers

rahul07

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)
sandhya6gczb
Processing the contents of /proc/cpuinfo is needlessly baroque. Use nproc which is part of coreutils, so it should be available on most Linux installs.

Command nproc prints the number of processing units available to the current process, which may be less than the number of online processors.

To find the number of all installed cores/processors use nproc --all

On my 8-core machine:

$ nproc --all
8

Login / Signup to Answer the Question.