find CPU Cores on Linux

4 Ways to Find CPU Cores on Linux

The ability to find CPU cores on Linux could be helpful in not just finding out the processing power of your system, but adding compatible hardware too. We’re here to show you not just one but four different ways of figuring out the CPU cores on your Linux OS system. 

The number of cores in consumer CPUs has been increasing rapidly in the past few years, with some reaching upwards of 16 cores. Having more cores is an upside but sometimes, having too many can be detrimental to our performance depending on the tasks we are trying to run.

Most consumers don’t need CPUs that have more than eight cores. The increase in core count also brought a decrease in single-core performance. And considering most consumer programs don’t use that many cores but rely solely on single-core performance—it is often better to go with a lower core count CPU that has a higher clock count.

4 Ways to Find CPU Cores on Linux 

Whether you’re looking to optimize your code, see if you can run that new AAA game, or just want to know how many cores your CPU has, here are four simple ways to check how many cores your CPU has.

Using the lscpu command

If we run the lscpu command, we are bombarded with all kinds of information regarding our CPU. We can see our architecture, model name, cache, and, most importantly, our core count.

lscpu command

We can make this a bit cleaner by running the egrep command alongside lscpu to get just our core count, as shown in the example below.

$ lscpu | egrep 'CPU\(s\)'

Using the egrep command here, we input our search term, and we add the backslashes for the brackets. Otherwise, the command won’t know if we want to include those characters in the search.

The number we are given here is the number of our logical cores, not our physical cores. In our case, we have a six-core CPU with hyperthreading. This means we have six physical and six logical cores, or you could even call them imaginary cores. 

This means that one core in this CPU can act like two cores, thus multiplying the overall core count.

Reading information from the /proc/cpuinfo file

Inside the /proc directory, we have a bunch of valuable files containing information about the currently running processes and details of other systems. 

The /proc directory is a virtual filesystem, sometimes referred to as a process information pseudo-file system. It is created upon system boot, destroyed on shutdown, and regarded as a control and information center for the kernel.

Using the cat command, we can read information about our CPU from the cpuinfo file.

Input:

$ cat /proc/cpuinfo

After doing that, we will be greeted with a bunch of information, similar to what lscpu gives us but much more. We can clean this up by using the grep command again.

Input:

$ cat /proc/cpuinfo | grep -m 1 'cpu cores'

Again using the egrep command, we just input our search term and add the -m flag and the input 1 for that flag. This means we simply want to see the first result presented to us. We do this because there are multiple results for this search term, although they all contain the same value, ‘6’.

As we can see here, unlike in the previous example, we are only shown the number of our physical cores.

Using the nproc command

This one is by far the easiest. If we look at the manual page for the nproc command, we can see that its primary function is to show the number of available cores.

Input:

Find CPU Cores on Linux

And if we run nproc.

$ nproc

Again, we will be shown the number of physical and logical cores.

Using htop/top

Lastly, we can check how many cores we have by running a simple CLI utility like htop or top. This way, not only do we get to see how many cores we have, but we are also able to see their utilization in real-time.

Find CPU Cores on Linux

We could see four different ways to look up the number of CPU cores on a system. Along with how to combine that with the use of the grep/egrep commands. These processes can save us time from searching for our preferred result manually and simply having it presented to us.

All of these commands have pros and cons. Some show us the total number of logical cores, while others only show us the number of our physical cores. And, of course, htop showing us the individual utilization of every core.

If you’re looking to upgrade your current CPU, we would recommend going for a CPU that has anywhere between four and eight cores if your primary goal is coding, gaming, and possibly some light production work. 

Generally speaking, you don’t need more than that unless you are doing some heavy rendering/testing, and even then, it’s much better to get a higher-end GPU that will do those tasks much faster.

If this guide helped you, please share it.

Leave a Reply
Related Posts