The “lsof” Command in Linux [8 Practical Examples]

The lsof command in Linux is an easy command. It is used to display the list of files that are opened on a system. Basically using this command you can view the information of the opened files on the system. The “lsof” stands for “list open files”. In this article, I will explain to you the ins & outs of the lsof command in Linux.

A. Description

The lsof command displays the list of opened files in Linux. It also displays opened files for specific users or specific processes. You can also view the list of opened files in a specific directory. Each line of the output represents one open file and the columns represent the information about the opened files.

B. Syntax

The lsof command in Linux has relatively straightforward syntax. The syntax is as follows:

lsof [OPTION]

Note: In the syntax above, OPTION is enclosed by a square bracket representing that one option can be used at the same time.

C. Options

A good quantity of options is available for the lsof command. I have listed some useful options for the command here. Yet, you can explore the man page for the lsof command to know more about its options.

man lsof

Useful Options

  • -u, Displays the list of opened files for a specific user.
  • -p, Shows the list of opened files for a specific process.
  • -i, Shows the list of files opened by network connections.
  • -R, Displays the list of files opened by parent process id.

Note: The options in Linux CLI (Command Line Interface) are case-sensitive, so be alert while using them.

Practical Examples of the “lsof” Command in Linux

The lsof command in Linux is a simple command which will show the list of open files. I have shown some useful examples of the lsof command here.

Example 1: Display List of All Open Files for All Processes Using the “lsof” Command in Linux

To display the list of all opened files I am using the lsof command in Linux. You can also do the same by following the below instructions:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Write the following command in the command prompt:

lsof

➌ Now, press the ENTER button.

Output >

In the image below, you can see the list of opened files displayed here.Display List of All Open Files for All Processes Using the “lsof” Command in Linux

Display List of All Open Files for All Processes Using the “lsof” Command in Linux


Similar Readings


Example 2: Display List of All Open Files for Specific User

To display the list of opened files for specific users you can use the lsof command in Linux. Here I want to display the list of all opened files for the user named sylvie. To display the list of opened files of your desired user, follow the steps below:

Steps to Follow >

➊ First launch the Ubuntu Terminal.

➋ Type the following command in the command prompt:

lsof -u sylvie

➌ Then, press the ENTER button.

Output >

In the following image, you can see the list of opened files for the user sylvie are shown here.Display List of All Open Files for Specific User

Display List of All Open Files for Specific User

Example 3: View List of All Open Files for All Except Specific User

To view the list of opened files for all except specific users I am using the lsof command in Linux. Here I want to display the list of all opened files for all users except the user sylvie. To do the same you can follow the process below:

Steps to Follow >

➊ Open the Ubuntu Terminal.

➋ Copy the following command in the command prompt:

lsof -u ^sylvie

➌ Finally, strike the ENTER key.

Output >

In the following image, the output displays the list of opened files for all except the user sylvie.View List of All Open Files for All Except Specific User

Example 4: Show a List of All Open Files for Specific Process ID Using the “lsof” Command in Linux

To show the list of opened files for specific process id I am using the lsof command in Linux. Here I want to show the list of all opened files for a specific process id which is 26. To do this you can follow the procedure below:

Steps to Follow >

➊ Initially open the Terminal in Ubuntu.

➋ Copy the following command in the command prompt:

lsof -p 26

➌ After that, hit the ENTER button.

Output >

In the image below, the output displays the list of opened files for process id 26.Show List of All Open Files for Specific Process ID Using the “lsof” Command in Linux

Example 5: View List of All Open Files for All Except Specific Process ID

To view the list of opened files for all except specific process id I am using the lsof command in Linux. Here I want to show the list of all opened files for all except a specific process id which is 26. To do this you can follow the instructions below:

Steps to Follow >

➊ To start, launch the Terminal in Ubuntu.

➋ Copy the following command in the command prompt:

lsof -p ^26

➌ Finally, strike the ENTER key.

Output >

As you can see in the image below, the list of opened files for all except process id 26 are shown here.View List of All Open Files for All Except Specific Process ID

View List of All Open Files for All Except Specific Process ID


Similar Readings


Example 6: Display a List of All Files Opened by Network Connections

I am using the lsof command to display the list of files opened by network connections in Linux. Here I want to display the list of all files opened by tcp protocol. You can do the same by following the steps below:

Steps to Follow >

➊ To initiate open the Terminal in Ubuntu.

➋ Write the following command in the command prompt:

sudo lsof -i tcp

➌ Now, press the ENTER key.

Output >

As you can see in the following image, the list of all files opened by the network connections are displayed here.Display List of All Files Opened by Network Connections

Example 7: Display List of All Open Files of Specific Directory Using the “lsof” Command in Linux

I am using the lsof command to display the list of opened files of a specific directory in Linux.  Here I want to display the list of opened files of the directory named sylvie. So If you want to do the same follow the steps below:

Steps to Follow >

➊ To start open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

lsof /home/sylvie

➌ Then, press the ENTER key.

Output >

As you can see in the image below, the output shows the list of all opened files of the directory named sylvie.Display List of All Open Files of Specific Directory Using the “lsof” Command in Linux

Example 8: Show List of All Files Opened by Parent Process ID Using the “lsof” Command in Linux

To show the list of files opened by the parent process id I am using the lsof command in Linux. So to do this you can follow the instructions below:

Steps to Follow >

➊ Initially launch the Ubuntu Terminal.

➋ Copy the following command in the command prompt:

lsof -R

➌ Finally, tap the ENTER button.

Output >

In the image below, the output displays the list of files opened by the parent process id.Show List of All Files Opened by Parent Process ID Using the “lsof” Command in Linux

Show List of All Files Opened by Parent Process ID Using the “lsof” Command in Linux

Conclusion

As explained in this article, the lsof command has various uses in Linux. You’ve also found the syntax, some functional options, and the practical applications of this command. To become a power user in Linux, have a glance at the command and its practical applications thoroughly.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Jannatul Ferdousi Sylvie

Hi there! This is Jannatul Ferdousi Sylvie. I am one of the Linux Content Developer Executives at Softeko. I live in Dhaka, Bangladesh. I have completed my BSc. in Software Engineering from American International University-Bangladesh. You can see my projects in the GitHub account. I love traveling, shopping, cooking, and gardening. Read Full Bio

Leave a Comment