How to Use the Linux lsof Command

Share
  • June 26, 2019

Fatmawati Achmad Zaenuri/Shutterstock.com

If everything in Linux is a file, there has to be more to it than just files on your hard drive. This tutorial will show you how to use lsof to see all the other devices and processes that are being handled as files.

On Linux, Everything Is a File

The oft-quoted phrase that everything in Linux is a file is sort of true. A file is a collection of bytes. When they are being read into a program or sent to a printer, they appear to generate a stream of bytes. When they are being written to, they accept a stream of bytes.

Many other system components accept or generate streams of bytes, such as keyboards, socket connections, printers, and communication processes. Because they either accept, generate, or accept and generate byte streams, these devices can be handled—at a very low level—as though they were files.

This design concept simplified the implementation of the Unix operating system. It meant that a small set of handlers, tools, and APIs could be created to handle a wide range of different resources.

The data and program files that reside on your hard disk are plain old filesystem files. We can use the ls command to list them and find out some details about them.

How do we find out about all the other processes and devices that are being treated as though they were files? We use the lsof command. This lists the open files in the system. That is, it lists anything that is being handled as though it were a file.

RELATED: What Does “Everything Is a File” Mean in Linux?

The lsof Command

Many of the processes or devices that lsof can report on belong to root or were launched by root, so you will need to use the sudo command with lsof.

And because this listing will be very long, we are going to pipe it through less .

sudo lsof | less

Read the remaining 89 paragraphs

Source : How to Use the Linux lsof Command