Bash Wiki
Posts (Latest 30 updated) :
Read all
Contents:
  1. [Linux] Bash lsof Uso: List open files and their associated processes
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
    5. Tips

[Linux] Bash lsof Uso: List open files and their associated processes

Overview

The lsof command stands for “list open files.” It is a powerful utility in Unix-like operating systems that allows users to view a list of all open files and the processes that opened them. This can be particularly useful for troubleshooting issues related to file access, monitoring system performance, or identifying which processes are using specific files.

Usage

The basic syntax of the lsof command is as follows:

lsof [options] [arguments]

Common Options

  • -a: AND list the specified options.
  • -c <command>: Show files opened by the specified command.
  • -u <user>: Show files opened by the specified user.
  • -p <PID>: Show files opened by the specified process ID.
  • -i: Show network files (internet connections).
  • +D <directory>: List all files opened in the specified directory.

Common Examples

Here are some practical examples of using the lsof command:

  1. List all open files:
    lsof
    
  2. List open files for a specific user:
    lsof -u username
    
  3. List open files for a specific process ID:
    lsof -p 1234
    
  4. List all network connections:
    lsof -i
    
  5. Find which process is using a specific file:
    lsof /path/to/file
    
  6. List all files opened in a specific directory:
    lsof +D /path/to/directory
    

Tips

  • Use lsof with grep to filter results for specific keywords. For example:
    lsof | grep keyword
    
  • Combine options to narrow down your search. For example, to find files opened by a specific user and command:
    lsof -u username -c command
    
  • Regularly check for open files to manage system resources effectively, especially on servers with limited file descriptors.