How to find number of CPU cores in Linux

In this article, we will see how to find number of CPU cores in Linux with different methods.

One can obtain the number of CPUs or cores in Linux from the command line. The /proc/cpuinfo file stores CPU and system architecture dependent items, for each supported architecture. We have to look at sockets and cores per socket.

Find the number of CPU cores in Linux

We have different methods to find the no of cpu cores in Linux. We are going to see few of them.

cat or more or grep of /proc/cpuinfo nproc command lscpu command getconf _NPROCESSORS_ONLN command

1. By viewing the contents of /proc/cpuinfo using cat or more commands.

cat /proc/cpuinfo

or

more /proc/cpuinfo

or

grep -c processor /proc/cpuinfo

2. Using nproc

nproc

How to find number of CPU cores in Linux

3. Using lscpu command.

echo "Cores = $(( $(lscpu | awk '/^Socket\(s\)/{ print $2 }') * $(lscpu | awk '/^Core\(s\) per socket/{ print $4 }') ))" or lscpu --all --parse=CPU,SOCKET,CORE | grep -v '^#'

4. Using getconf _NPROCESSORS_ONLN command. It is part of glibc.

getconf _NPROCESSORS_ONLN

Please watch the below video for the same above steps.

Leave a Reply

Your email address will not be published. Required fields are marked *