Linux lscpu Command Tutorial for Beginners (5 Examples)

We've already discussed the lshw command, which fetches data related to your system's hardware. However, if you are only interested in CPU-related information, there's a dedicated command line utility for that was well: lscpu. In this article, we will discuss this tool using some easy to understand examples.

But before we do that, it's worth mentioning that all examples in this tutorial have been tested on Ubuntu 16.04 LTS.

Linux lscpu command

The lscpu command in Linux displays information about the CPU architecture.  Here's the syntax of the tool:

lscpu [-a|-b|-c] [-x] [-s directory] [-e[=list]|-p[=list]]
lscpu -h|-V

And here's what the man page says about it:

       lscpu  gathers  CPU  architecture  information from sysfs and /proc/cpuinfo.  The command 
output can be optimized for parsing or for easy readability by humans.  The information
includes, for example, the number of CPUs, threads,  cores,  sockets,  and  Non-Uniform
       Memory  Access (NUMA) nodes. There is also information about the CPU caches and cache
sharing, family, model, bogoMIPS, byte order, and stepping.

       Options that result in an output table have a list argument. Use this argument to customize
the command output. Specify a comma separated list of column labels to limit the output
table to only the specified columns, arranged in the specified order. See COLUMNS for a
list of valid column labels. The column labels are not case sensitive.

       Not all columns are supported on all architectures. If an unsupported column is specified,
lscpu prints the column but does not provide any data for it.

The following Q&A-styled examples should give you a better idea on how this tool works.

Q1. How to use lscpu command?

Basic usage is pretty straight forward - all you have to do is to run the 'lscpu' command sans any options.

lscpu

How to use lscpu command

Q2. How to make lscpu display information in human readable format?

There exists a specific command line option -e that lets you do this.

lscpu -e

For example, here's what the aforementioned command produced in output:

How to make lscpu display information in human readable format

If you want, you can also limit the output to specific columns. For example, the following command limits the output to CPU and NODE columns.

lscpu -e=cpu,node

lscpu command formatted result

Q3. How to limit the output to only online or offline CPUs?

The lscpu command lets you limit the output it produces to only online or offline CPUs. You can do this using the -b and -c command line options, respectively.

lscpu -b
lscpu -c

Note that both these command line options may only be specified together with option -e or -p.

Q4. How to make lscpu display output in parsing-friendly format?

You can do this using the -p command line option.

lscpu -p

Following is the output this command produced in my case:

How to make lscpu display output in parsing-friendly format

Note that just like in the case of -e option, you can also limit the output in this case by passing the 'list' argument.

--parse[=list]

Here's what the man page has to say about the 'list' argument:

If the list argument is omitted, the command output is compatible with earlier versions of lscpu. 
In this compatible format, two commas are used to separate CPU cache columns. If no CPU caches are
identified the cache column is omitted.

If the list argument is used, cache columns are separated with a colon (:). When specifying the
list argument, the string of option, equal sign (=), and list must not contain any blanks or other
whitespace. Examples: '-p=cpu,node' or '--parse=cpu,node'.

Q5. How to make lscpu use hexadecimal masks for CPU sets?

By default, lscpu prints CPU sets in list format (for example 0,1). However, if you want, you can have lscpu use hexadecimal masks for CPU sets, something which you can do using the -x command line option.

lscpu -x

How to make lscpu use hexadecimal masks for CPU sets

Conclusion

Like lshw, the lscpu command is also not something that you'll require everyday, but it may save your day when the requirement arises. Here, in this tutorial, we have discussed majority of features (command line options) it offers, so practice these, and when done, head to the tool's man page to know more.

Share this page:

2 Comment(s)