Linux Server

Learn Using lsof Command in Linux with Examples – Serverwala

Introduction 

Last updated on September 3rd, 2022

lsof command in Linux stands for ‘List Open Files’. It is utilized to figure out or get the information about the files that get opened up through the different processes. Linux/Unix view and hold everything as a file and further, maintains the folder. When you work with Linux/Unix system, there are numerous files(devices, directories, pipes, sockets, etc.) and folders that get employed. Some of them can be visible to you and some not. In a single execution, the lsof command can list out all the open files in the output console. You do not only obtain a list of standard usual files but also can acquire a list of a directory, a named pipe, a block special file, a shared library, a UNIX domain socket, a character special file, an internet socket, and various more by using lsof command. 

This command is also helpful when a disk cannot be unmounted due to the files in use. Using lsof, you can easily recognize the files that are being used and are not letting you unmount the disk. Moreover, you can also employ this command with the grep command in order to perform advanced searching and listing. We have come up with this guide to make you learn the usage of the lsof command. We will also be explaining through examples for easy understanding. Jump to the subsequent sections to begin learning the significant uses of lsof command.

Read More: Create a New File in Linux Using the Command Line

lsof Command Syntax

Before, you move to learn executing lsof for different purposes, let us highlight the basic syntax of lsof command in Linux – 

$lsof [option][user name]

Usage of lsof command in Linux 

To list all open files 

You need to enter the following command in order to get the list of all the files opened by any process in the system. 

~$ lsof

After entering this command, you can have all the details of files that are opened, including process Id, the user linked with the process, size, etc. Moreover, the output will be self-explanatory. However, let us examine FD & TYPE columns more precisely –

FD is an acronym used for Linux File Descriptor and can incorporate the following values – 

  • cwd – It represents the Current Working Directory.
  • txt – It refers to the program Text file (code and data).
  • mem – It represents the Memory file.
  • mmap – It refers to the Memory-mapped device.
  • rtd – It represents Root Directory. 

Furthermore, FD column numbers such as 1u are actual file descriptors. They are followed by u,r,w of its mode as –

  1. r for reading access.
  2. w for writing access.
  3. u for reading and writing access.

To list all the files that are specifically opened by the user

In a system, there can be numerous users who possess distinct needs. Thus, they make use of different files and devices. If you wish to view a list of files that are opened by any specific user, you can enter the command as –

lsof -u username

In the place of a username, you need to enter the name of that specific user. Further, you can encounter the type of file in the output as –

  • DIR: It represents Directory.
  • REG: It represents a Regular file.
  • CHR: It represents Character special file.

To list all the files opened by everyone except a particular user 

There can be times when you need to see the list of files that are opened by everyone on the system except a specific user. In such a case, you can make use of the command as –

lsof -u ^username

Here, the username is the name of the user whose opened files you wish to discard from getting listed in the output. 

To list all the files opened by a particular process

The following command syntax can help you in listing out all the files that are opened by a particular process. In this syntax, the is -c is followed by the process name, and the files opened through this particular process will get listed in the output.

lsof -c process_name

For instance, lsof -c ssh

By entering this command, you can obtain all the files along with their description opened by the sshl process. 

To list all the opened files by a particular process ID

Each file on the system is linked with some process ID. You can encounter a number of files opened by a particular process. So, lsof -p is utilized for listing down the files opened by a particular process ID. The lsof command in Linux that you need to enter for finding the list of all the opened files by a specific process ID is- 

lsof -p process ID

To list all the files opened by other PID

You have learned how to list all the files opened by a particular or specific process ID. In a similar order, you can make use of the following command in order to figure out the list of files that are not opened by a particular process ID –

lsof -p ^process ID

To list all the files opened by parent process IDs

In a system, an extensive range of processes run. And, thus, there is a large array of files opened for their utilization. Moreover, a single process can have multiple child processes. Such a process can be termed the parent process. So, for finding the list of files that are opened by parent process Id, you need to employ lsof command along with the option -R as –

$lsof -R

To list all the files opened by a directory

You can also list all the files that are being opened by a specific directory. You must be aware that in a system, there are files as well as a directory. So, one can find several files that are opened either by directory or a regular file. In order to list all the files opened by a directory, enter the command –

$lsof -D directory path

To list all the files opened by network connections

Your personal computer or system can possess connections with several networks, which assists in a number of purposes. As we have discussed that in the Linux system, everything functions as a file, so you can list out the files opened by network connections as well. The lsof command in Linux that you need to employ here is –  

$lsof -i

Summing Up

You must have understood the significance and usage of the lsof command in Linux by going through this tutorial. Moreover, if you want to dig deeper and get further insights about lsof command, you can view its manual page. To view lsof Linux manual page, you can enter the following command – 

$ man lsof

Furthermore, we at Serverwala offer high-end customer support with expertise and quick response. You can always connect with our Serverwala experts while finding any difficulty in the execution process. They are available via different communication mediums to render prompt support and solutions. 

Read More: How to Check Disk Space on Linux?

Get in Touch

Arpit Saini

He is the Director of Cloud Operations at Serverwala Cloud Data Centers Pvt Ltd and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles

Adblock Detected

Please consider supporting us by disabling your ad blocker