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.
Press Ctrl + Shift + Esc to open Task Manager. Select the Performance tab to see how many cores and logical processors your PC has.
Open a terminal. 2. Use the cat command to display the data held in /proc/cpuinfo. This command will produce a lot of text, typically it will repeat the same information for the number of cores present in your CPU. A more concise means to get most of this information is via lscpu, a command that lists the CPU details.
Here, the CPU(s) value indicates the number of logical cores, which is equal to 8 in our output. The number of logical cores is equal to “Thread(s) per core” × “Core(s) per socket” × “Socket(s)” and the number of physical cores on a machine equals “Core(s) per socket” × “Socket(s)”.
CPU(s): 4. Core(s) per socket: 4. CPU family: 6.
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
)
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
The most portable solution I have found is the getconf
command:
getconf _NPROCESSORS_ONLN
This works on both Linux and Mac OS X. Another benefit of this over some of the other approaches is that getconf has been around for a long time. Some of the older Linux machines I have to do development on don't have the nproc
or lscpu
commands available, but they have getconf
.
Editor's note: While the getconf
utility is POSIX-mandated, the specific _NPROCESSORS_ONLN
and _NPROCESSORS_CONF
values are not.
That said, as stated, they work on Linux platforms as well as on macOS; on FreeBSD/PC-BSD, you must omit the leading _
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With