The Wayback Machine - https://web.archive.org/web/20240930171511/https://www.geeksforgeeks.org/lsof-command-in-linux-with-examples/
Open In App

How to List Open Files in Linux | lsof Command

Last Updated : 23 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In the world of Linux, understanding and managing open files is crucial for system administrators and users alike. The Linux operating system provides a powerful utility called lsof (List Open Files) that allows users to gain insights into the files currently open on their system. In this article, we will delve into the intricacies of the lsof command, exploring its syntax, options, and practical use cases.

Linux/Unix considers everything as a file and maintains a folder. So “Files or a File” is very important in Linux/Unix. While working in a Linux/Unix system there might be several files and folders that are being used, some of them would be visible and some not. 

What Is lsof and Why Is It Important?

lsof command stands for List Open Files. This command provides a list of files that are opened. Basically, it gives the information to find out the files which are opened by which process. With one go it lists out all open files in the output console. It can be combined with ‘grep’ command and can be used to do advanced searching and listing. 

These files can include:

  • Regular files (e.g., text or binary files)
  • Directories
  • Block special files (e.g., devices)
  • Character special files
  • Pipes
  • Sockets (both UNIX and internet)
  • Network connections (TCP and UDP)

With lsof, you can find out which processes are accessing which files, which is extremely useful in a range of scenarios, such as:

  • Diagnosing resource usage issues
  • Debugging applications that open multiple files
  • Monitoring network connections
  • Managing file locks and permissions

Syntax of List Open Files `lsof`

The basic syntax of the `lsof` command is as follows:

lsof [option]

here,

  • lsof: The command itself, used to list open files.
  • [options]: Various flags that allow you to customize the behavior of lsof, filtering results by user, process ID, file type, and more.

Common Options Available in ‘lsof’ Command

Option Description
-c List files opened by a specific process name.
-u Display files opened by a specified user.
-i Show network-related information.
-p List files opened by a specific process ID (PID).
-t Display only the process IDs (PIDs).
-D List files opened by a specific directory.
-R List parent process IDs along with their child processes.

Pratical Example of How to List Open File in Linux

1. How to list all open files on the system

List all open files: This command lists out all the files that are opened by any process in the system. 

lsof

How to list all open files on the system

Here, you observe there are details of files which are opened. Process Id, the user associated with the process, FD(file descriptor), size of the file all together gives detailed information about the file opened by the command, process ID, user, its size etc. 

  • FD represents as File descriptor.
  • cwd : Current working directory.
  • txt : Text file.
  • mem : Memory file.
  • mmap : Memory mapped device.

2. How to show file opened by a particular user

List all files opened by a user: There are several users of a system and each user have different requirements and accordingly they use files and devices. To find a list of files that are opened by a specific user this command is useful. 

Syntax: 

lsof -u username

Replace “username” with the desired username. This command provides a list of files opened by the specified user.

List all files opened by a user

In the figure given above with the command lsof -u ubuntu lists out all the files opened by ubuntu user. Along with that we can see the type of file here and they are: 

  • DIR: Directory
  • REG: Regular file
  • CHR: Character special file

3. How to List all files which are opened by everyone except a specific user

With the help of this command you can list out all the files opened by all the process and all the user. But when we want to find the list of files that are opened by all users except a particular user then we can use: 

Syntax: 

lsof -u ^root

List all files  which are opened by everyone except a specific user

In the given figure we can observe there are no files that are opened by the root user.

4. How to list all open files by a particular process

This command can list out all the files opened by a particular process. ‘-c’ followed by process names can find out all the files that are opened by that particular process that is named in the command. 

Syntax: 

lsof -c Mysql

list all open files by a particular process

Here, you can observe that the files and their description opened by Mysql process. Another example is the files that are opened by the apache process: 

list all open files by a particular process

5. How to List all open files that are opened by a particular process ID

Each file is associated with some process ID. There can be many files that are opened by a particular process. By using lsof -p process ID, files opened by a particular process can be checked. 

Syntax: 

lsof -p process ID

List all open files that are opened by a particular process ID

6. Files opened by all other PID

As the above-given figure command lists out the files opened by a particular process ID. In the same way, you can use below command option to find out the list of files which are not opened by a particular process ID. 

Syntax: 

lsof -p ^process ID

Files opened by all other PID

7. How to List parent process IDs

There is a large number of process running in a system and they have files opened for its usage. There may be many child processes of a process and this process can also be termed as the parent process. To find out the list of files opened by parent process Id lsof command is used with the option -R. 

Syntax: 

lsof -R

List parent process IDs

8. How to list all opened files opened by a directory

It lists out the files which are opened by a particular directory. There are files as well as the directory in a system. So there can be several files opened by a directory as well as the regular file. 

Syntax: 

lsof -D directory path

list all opened files opened by a directory

9. How to open Files by network connections

Our Pc/system can be connected through various networks which helps in a variety of purpose. As we know that in Linux everything is a file, so we can even check the files that are opened by some network connections in the system. 

Syntax: 

lsof -i

open Files by network connections

Here in the figure, we can see the files opened by the TCP network. In the same way, we can check for UDP etc.

Note: To know more in details about the lsof command you can see the manual page as follows:  

man lsof

open Files by network connections

Conclusion

In this article, we’ve explored the power of the lsof command in Linux, learning how to list open files, processes, and network connections. The ability to monitor open files is essential for troubleshooting, security, and system performance management. By understanding how to use lsof with its various options, you can gain valuable insights into the behavior of your Linux system and optimize its resource usage.

How to List Open Files in Linux – FAQs

What is the lsof command used for in Linux?

The lsof command (List Open Files) is used to display information about open files and the processes that are using them on a Linux system. This includes regular files, directories, network connections, pipes, and more.

How do I list all open files on my system using lsof?

To list all open files on your system, simply run the lsof command without any options:

lsof

Can I check which files are opened by a specific user?

Yes, to list files opened by a specific user, use the -u option followed by the username:

lsof -u username

Replace username with the actual username you want to query.

How do I exclude files opened by a specific user?

To list all open files except those opened by a specific user, use the caret (^) symbol before the username:

lsof -u ^username


Previous Article
Next Article

Similar Reads

How to Set Nginx Max Open Files in Linux
In Linux, the Nginx depends on the system limits for maximum open files to efficiently handle the parallel connections and to serve the web content. The maximum open file limit determines the number of files, which includes the sockets, that Nginx can simultaneously have opened. We can adjust this limit as per the necessity to accommodate the incre
4 min read
How to Fix the "Too Many Open Files" Error in Linux
Encountering the "Too Many Open Files" error in Linux can be frustrating, especially when it disrupts your workflow or server operations. This error typically occurs when a process or system reaches its limit for open file descriptors, which are references to open files or resources such as sockets, pipes, or device files. Fortunately, resolving th
5 min read
How to open ISO files on Ubuntu Linux
ISO files are disc image archives commonly used to distribute software and operating systems. There are two main ways to access the contents of an ISO file on Ubuntu Linux: using the graphical user interface (GUI) or the command line. What is an ISO File?An ISO file or an ISO image is a single data file that incorporates all the data that is presen
5 min read
Open Google Chrome From The Linux Command Line
Google Chrome is known as the most used and widely popular web browser because of its fast and better performance. In this article we will learn how we can open the Google Chrome application by using the terminal in Linux along with this we will also look at some you should command line switches to help you to start using the Google Chrome browser
6 min read
xdg-open command in Linux with Examples
xdg-open command in the Linux system is used to open a file or URL in the user’s preferred application. The URL will be opened in the user’s preferred web browser if a URL is provided. The file will be opened in the preferred application for files of that type if a file is provided. xdg-open supports ftp, file, https, and http URLs. This can be use
2 min read
Linux - Installing locate Command to Find Files
In this article, we will see how to install locate command to find files in Linux. locate is a command-line interface tool used for finding files by name in Linux systems. This works more efficiently than other commands. It uses one or more databases by updatedb. To check whether locate utility is already installed in your system. Open up your term
2 min read
How to Securely Copy Files in Linux | scp Command
scp (secure copy) command in Linux system is used to copy file(s) between servers in a secure way. The SCP command or secure copy allows the secure transferring of files between the local host and the remote host or between two remote hosts. It uses the same authentication and security as it is used in the Secure Shell (SSH) protocol. SCP is known
8 min read
How to Copy Files and Directories in Linux | cp Command
In the world of Linux computers, a common and important job is copying files. The key tool for this task is the "cp" command. In this simple guide, we'll explore how to copy a file in Linux, looking at the basic steps, different choices you can make, and giving clear examples. We'll also take a peek behind the scenes to understand how the "cp" comm
8 min read
How to Compress Files in Linux | Tar Command
File compression is a fundamental task in managing and transferring data efficiently on a Linux system. The Tar command, short for Tape Archive, is a powerful tool that allows users to create compressed and archived files. In this comprehensive guide, we will explore the various options and examples of using the Tar command to compress files on a L
11 min read
How to Compare Files Line by Line in Linux | diff Command
In the world of Linux, managing and comparing files is a common task for system administrators and developers alike. The ability to compare files line by line is crucial for identifying differences, debugging code, and ensuring the integrity of data. One powerful tool that facilitates this process is the diff command. In this article, we will explo
9 min read
How to sort lines in text files in Linux | sort Command
SORT command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ASCII. Using options in the sort command can also be used to sort numerically.  SORT command sorts the contents of a text file, line by line.sort is a standard command-line program that prints the lines
7 min read
How to Compress and Extract Files Using the tar Command on Linux
Tar archives are special files that bundle multiple files and directories into a single file, making it easier to store, share, and manage. While tar archives themselves are not compressed, they can be combined with various compression utilities to reduce their size. The tar command is highly versatile, allowing users to create, modify, and extract
3 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
Open and Run Python Files in the Terminal
The Linux terminal offers a powerful environment for working with Python files, providing developers with efficient ways to open, edit, and run Python scripts directly from the command line. Open and Run Python Files in the Linux TerminalIn this article, we'll explore various techniques and commands for handling Python files in the Linux terminal,
1 min read
Ways to Find Out List of All Open Ports in Linux
In this guide, we'll explore how to identify the comprehensive list of open ports in Linux, crucial endpoints for communication within computer networks. Ports, serving as gateways for network communication, are represented by 16-bit numbers ranging from 0 to 65535. These ports play a pivotal role in facilitating communication through Internet tran
4 min read
fc-list command in Linux with examples
fc-list command is a part of the fontconfig system. It is used to list the available fonts and font styles. Using the format option, the list of all fonts can be filtered and sorted out. Moreover, multiple parameters can be passed to it after a colon symbol (:) to restrict the information that is being displayed on the screen. Syntax: fc-list [-vqV
2 min read
How to List Current Logged-In Users in Linux | who Command
Linux, being a powerful and versatile operating system, provides several command-line tools to manage and monitor user activity. One common administrative task is to identify and list the currently logged-in users on a Linux system. In this article, we will delve into multiple ways to achieve this, exploring commands such as who, user, w, last, fin
5 min read
How to List Running Processes in Linux | ps Command
As we all know Linux is a multitasking and multi-user system. So, it allows multiple processes to operate simultaneously without interfering with each other. Process is one of the important fundamental concepts of the Linux OS. A process is an executing instance of a program that carries out different tasks within the operating system.  Table of Co
9 min read
How to List All Block Devices in Linux | lsblk Command
Understanding the storage devices connected to your Linux system is crucial for efficient system management. The 'lsblk' command, short for "list block devices," is a powerful tool that provides detailed information about block devices such as hard drives, solid-state drives, and other storage-related devices. Here, we will look into the 'lsblk' co
7 min read
Cansina - Open Source Hidden Content Discovery Tool on Linux
Reconnaissance is one of the first steps to conduct within a pen test engagement. During this stage, information is gathered using different tools and sources. Some web applications may hide web resources from the public, there is however a way to discover the hidden content. This hidden content can contain some sensitive data about the internal ar
4 min read
lazyGrandma - Open Source Libs in Linux
LazyGrandma tool is one of the outstanding Recon tools which gathers information from around 50+ online websites. This tool divides the recon process into 10 different waves which include Subdomain Enumeration, Port Scanning, Google Dorks, and many more. Whenever we run the tool the tool opens various websites that hold the respective wave informat
2 min read
Converting Images to webp Open Source Format in Linux
In this article, we are going to see how to convert images to webp open-source format in Linux. Before starting, we will see the webp and its working process. What is WebP?It is a new image format for the web employing both loss and lossless compression.It is currently developed and supported by Google, based on technology acquired with the acquisi
5 min read
Installing Open Source TV Streaming Server TvHeadend in Linux Mint
A server program that reads video streams from LinuxTV sources and publishes them as internet streams is TVHeadend, often TVH for short. For example, it supports multiple inputs, a DVB-T USB tuner stick and a Sat > IP tuner, combining them into a single list of channels. TVH servers are IP signal providers themselves, allowing TVH server network
3 min read
How to Change the Number of Open File Limit in Linux?
If you are an active Linux user, who has to work with many files on Linux at a time then you might have definitely faced a problem regarding “Too many open files” on a Linux system. When you have reached the maximum open file limit you will get an error message displaying "Too many open files (24)” error on your screen. Why do we face such a thing?
4 min read
Quasar – An Open Source Information Gathering Tool in Kali Linux
Quasar is an open-source information gathering tool in Github. Quasar is an open-source intelligence and information gathering tool based on (OSINT). Quasar is capable of doing everything almost you need for performing reconnaissance. As per your need, it can perform reconnaissance easily. There are various modules that work in this tool. Using dif
4 min read
OSRFramework - Open Source Research Framework on Linux
OSINT is the most common method or technique for collecting information about the target domain or employee of the organization from open-source or publicly available data. Mostly malicious hackers use this technique in the attacks of Social Engineering, Phishing, etc. But on the good side, We can use this OSINT technique or understanding the scope
3 min read
OpenRedireX – Open Redirection Vulnerability Finder Tool in Linux
Open redirect is a security defect in an app or a web page that causes it to fail to properly authenticate URLs. When apps and web pages have requests for URLs, they are supposed to prove that those URLs are part of the expected page's domain. To test the web-based application manually for Open Redirection is very difficult. So we need an automated
4 min read
Oralyzer : Linux Tool To Identify Open Redirection
Open Redirection is the security vulnerability in a web-based application that causes it to fail to properly authenticate URLs. When any web-based application receives requests for URLs, they are supposed to prove that the requested URLs are an integral part of their domain. No other URLs should be redirected except the authenticated one. So there
4 min read
11 Best Open Source Note-Taking Apps for Linux
Note-taking apps have become a norm in the modern era of computing, and how good your note-taking app is, has a direct impact on your productivity. When it comes to Note-Taking apps on Linux, there are many free and open-source applications with their unique sets of features and capabilities. So in this article, we will explore and try to take a lo
8 min read
Article Tags :