nproc

systemLinux/Unix
The nproc command is one of the most frequently used commands in Linux/Unix-like operating systems. nproc Print the number of processing units available

Quick Reference

Command Name:

nproc

Category:

system

Platform:

Linux/Unix

Basic Usage:

nproc [options] [arguments]

Common Use Cases

    Syntax

    nproc [OPTION]

    Options

    Option Description
    --all Print the number of installed processors
    --ignore=N If possible, exclude N processing units
    --help Display help information and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the nproc command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    nproc
    Print the number of available processing units.
    nproc --all
    Print the number of installed processing units.
    # Advanced Examples Advanced
    nproc --ignore=2 Print the number of available processing units minus 2. threads=$(nproc) Store the number of available processing units in a variable. make -j$(nproc) Run make with parallel jobs equal to the number of processing units. make -j$(($(nproc)-1)) Run make with parallel jobs equal to the number of processing units minus 1. if [ $(nproc) -gt 4 ]; then echo "More than 4 cores available"; fi Check if more than 4 processing units are available. nproc --ignore=$(($(nproc) / 2)) Use half of the available processing units. for i in $(seq 1 $(nproc)); do echo "Starting worker $i"; done Start a number of workers equal to the number of processing units. python3 -c "import multiprocessing; print(multiprocessing.cpu_count())" Alternative way to get the number of CPUs in Python (for comparison).

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The nproc command is a simple yet useful utility that's part of the GNU Coreutils package. Its primary function is to display the number of processing units (CPU cores or threads) available to the current process. This information is particularly valuable for optimizing parallel processing tasks and workload management. Key features of the nproc command: 1. Available Processing Units: By default, nproc reports the number of processing units available to the current process, which may be less than the total number of processors installed on the system due to CPU affinity settings or resource limitations. 2. Total Processor Count: With the --all option, nproc displays the total number of processors installed on the system, regardless of availability to the current process. 3. Resource Reservation: The --ignore=N option allows users to exclude a specified number of processors from the count, which is useful for leaving resources available for other system processes. 4. Script Integration: nproc is designed to be easily integrated into shell scripts and command pipelines, making it perfect for dynamically adjusting parallel workloads based on system capabilities. Common use cases for nproc include: - Determining the optimal number of parallel jobs for build systems like make (-j flag) - Configuring application thread pools to match available system resources - Dynamically adjusting resource utilization in scripts based on the current system's capabilities - Setting appropriate concurrency levels for data processing tasks - Checking system specifications in diagnostic scripts While simple in function, nproc provides a standardized way to obtain processor count information across GNU/Linux systems. Before nproc became widely available, various less portable methods were used to obtain this information, such as parsing /proc/cpuinfo or using platform-specific commands. It's worth noting that nproc counts logical processors, which means that on systems with technologies like Intel's Hyper-Threading, each physical core may be counted as multiple processing units. For workloads that don't benefit from simultaneous multithreading, you might want to consider this when determining the optimal level of parallelism. In modern computing environments with containerization and virtualization, nproc will report the number of processors available to the container or virtual machine, not necessarily the host system's total processor count. Alternatives to nproc include checking the contents of /proc/cpuinfo on Linux systems, using platform-specific commands like lscpu, or utilizing programming language libraries that provide CPU information (such as Python's multiprocessing.cpu_count()).

    Related Commands

    These commands are frequently used alongside nproc or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the nproc command works in different scenarios.

    $ nproc
    View All Commands