Find the number of cpu cores in your linux system

Circuit Board closed up, a few computer chips visible.

Circuit Board closed up, a few computer chips visible.

I recently got a new work laptop and after having IT install WSL for me, I installed and ran the command htop, an interactive process viewer. I was amazed as I have never before had a computer with 16 cpu cores. I was thrilled!

Then I started thinking, how do I find that number using oneliners? I first tried the lscpu command, with the awesome pipe feature and unix philosophy that each program should do one thing well, I ended up with this combination:

lscpu | grep "CPU(s):" | awk '{ print $2 }'

This worked and gave me the number 16.

Then I remembered that /proc/cpuinfo exists and found the word siblings in there and tried this:

grep siblings /proc/cpuinfo | uniq | awk '{ print $3 }'

Which also gave me the number 16.

And after a search, I found that there is a command for this - nproc.

nproc

Which gave me the number 16. What a fun adventure that was, and I’m so glad I can pipe my way through the terminal command line interface universe. I’m sure there are other paths to the same result too.

But also, I’m glad to discover that there exist commands for almost anything. And if I cannot find that, there is always pipes.

“The pipes are calling…”