Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App

How to List Open Files in Linux | lsof Command

Last Updated : 23 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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

OptionDescription
-cList files opened by a specific process name.
-uDisplay files opened by a specified user.
-iShow network-related information.
-pList files opened by a specific process ID (PID).
-tDisplay only the process IDs (PIDs).
-DList files opened by a specific directory.
-RList 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.


B

Bhumika_Rani
Improve
Article Tags :
  • Linux-Unix

Similar Reads

    journalctl Command in Linux with Examples
    The `journalctl` command is part of the systemd suite of utilities and is used to query and display log messages from the systemd journal. The systemd journal is a centralized logging system that collects and stores log data from various sources, including system services, kernel events, and user ap
    7 min read
    How to Kill a Process in Linux | Kill Command
    kill command in Linux (located in /bin/kill), is a built-in command which is used to terminate processes manually. kill command sends a signal to a process that terminates the process. If the user doesn't specify any signal that is to be sent along with the kill command, then a default TERM signal i
    6 min read
    last command in Linux with Examples
    The 'last' command in Linux is a powerful utility used to display a list of all users who have logged in and out since the creation of the '/var/log/wtmp' file. It provides a chronological view of user sessions, allowing administrators to monitor user activity, troubleshoot issues, and manage system
    5 min read
    less command in Linux with Examples
    The 'less' command in Linux is an indispensable utility for browsing the contents of text files interactively. Unlike traditional text editors, 'less' allows you to view text files page by page without loading the entire file into memory. This approach not only speeds up the file loading process, es
    5 min read
    let command in Linux with Examples
    The `let` command in Linux is a powerful tool used for evaluating arithmetic expressions on shell variables. It supports various operators and functionalities to perform calculations and manipulate values. Syntax of `let` command in Linux The basic syntax of the `let` is as follows. let [expression]
    3 min read
    ln command in Linux with Examples
    The 'ln' command in Linux is a powerful utility that allows you to create links between files. These links can either be hard links or soft (symbolic) links. If you're unfamiliar with these concepts, check out our detailed guide on Hard and Soft Links in Linux to understand their differences, use ca
    3 min read
    locate command in Linux with Examples
    locate command in Linux is used to find the files by name. There are two most widely used file-searching utilities accessible to users called to find and locate. The locate utility works better and faster than the find command counterpart because instead of searching the file system when a file sear
    6 min read
    look command in Linux with Examples
    The look command in Linux is used to display lines that begin with a specified string. It is especially useful for searching through large files or lists, as it helps you locate entries efficiently. By default, the look command performs case-insensitive matches and searches for exact prefixes. The l
    3 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-relat
    6 min read
    lshw command in Linux with Examples
    The 'lshw' (List Hardware) command in Linux/Unix is a powerful tool for extracting detailed information about the system's hardware configuration. This tool retrieves data from files in the '/proc' directory and is capable of reporting on a wide array of components, including memory configuration, C
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences