lsof command

Basic Syntax:

1
lsof [options] [file|socket|keyword]

Options:

  • a: Selects files or sockets that match all of the specified keywords.

  • c: Specifies a process ID (PID) to filter results by. For example, lsof -c 12345 would show only files and sockets used by process with PID 12345.

  • d: Specifies a directory to search for open files or sockets.

  • e: Excludes files or sockets that match the specified keyword(s).

  • f: Forces lsof to use a specific file type (e.g., FIFO, socket, etc.).

  • i: Selects Internet sockets only (default is all types of files and sockets).

  • k: Specifies a keyword or pattern to search for in file names.

  • n: Disables hostname lookup for network addresses.

  • p: Specifies a process name or executable name to filter results by.

  • r: Reverses the order of the output (i.e., shows files and sockets most recently used first).

  • s: Shows only socket information.

  • t: Specifies a file type to search for (e.g., REGULAR, DIRECTORY, etc.).

  • u: Shows only user-level information (i.e., open files and sockets used by users).

  • w: Sets the minimum number of bytes required for an output line.

File/Socket/Keyword:

  • file: Specifies a file name or pattern to search for.

  • socket: Specifies a socket name or pattern to search for.

  • keyword: Specifies a keyword or pattern to search for in file names, socket names, or other metadata.

Examples:

  1. List all open files:
1
lsof
  1. List all Internet sockets:
1
lsof -i
  1. Find files and sockets used by process with PID 12345:
1
lsof -c 12345
  1. Find files and sockets that match a specific keyword (e.g., “ssh”):
1
lsof -k ssh
  1. Force lsof to search only for FIFO files:
1
lsof -f FIFO

Tips:

  • You can combine options to create more complex queries.

  • Use the a option with multiple keywords to find files or sockets that match all of them.

  • Be careful when using lsof, as it may show sensitive information (e.g., password files).

  • On Linux systems, you may need to use the sudo command to run lsof if you don’t have permission to access certain system resources.

请我喝杯咖啡吧~

支付宝
微信