How to Print the Number of Processing Units Using the nproc Command in Linux

When you are compiling a massive software project from source code (like a custom Linux kernel) using the make command, the compilation process can take hours if it is restricted to a single thread. To speed up the process, you can pass the -j flag to make, instructing it to run multiple compilation jobs in parallel. However, to optimize this perfectly, you need to know exactly how many processing units (CPU cores or threads) your server has available. You can find this information instantly using the Linux nproc command.

How the nproc Command Works

The nproc utility is incredibly simple. It is part of the standard GNU Core Utilities package, so it is pre-installed on virtually every Linux distribution. Its sole purpose is to print a single integer representing the number of processing units currently available to the current process.

To use it, open your terminal and type the command with no arguments:

nproc

If your server has a quad-core processor with hyper-threading enabled, the command will simply output the number 8.

Understanding “Available” Processing Units

It is important to understand that nproc does not blindly report the total physical hardware installed in the machine. It respects system-level resource limits (like taskset or cgroups).

For example, if you are running a script inside a Docker container that has been strictly limited to only use 2 CPU cores, running nproc inside that container will output 2, even if the host machine has 64 physical cores. This makes nproc incredibly reliable for automated scripts, as it guarantees it will not attempt to spawn more threads than the environment actually allows.

Querying Total Physical Hardware

If you are an administrator and you want to bypass these software limits to query the absolute total number of processing units physically installed on the motherboard, you can use the --all flag.

nproc --all

This flag ignores all environmental restrictions and prints the raw hardware count.

Automating Parallel Compilation

Because nproc outputs a clean, single integer, it is perfect for command substitution in bash scripts. Instead of manually checking your core count and then typing make -j 8, you can dynamically assign the core count to the make command.

make -j$(nproc)

This elegant one-liner will query the system for its maximum available processing units and instantly instruct the compiler to use exactly that many threads, ensuring maximum performance on any machine.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.