Skip to content

MacOS lsof command

The MacOS lsof command, short for “list open files,” is a powerful utility that allows users to list all open files and processes on their system. This command is particularly useful for troubleshooting issues related to file access, network connections, and resource sharing. By running the lsof command with different options and filters, users can obtain valuable information such as which processes have a particular file open, which network connections are established, and which files are being accessed.

One of the key features of lsof is its ability to provide real-time information about open files and processes, making it an essential tool for system administrators and advanced users. By understanding how to effectively use the lsof command, users can gain insights into the inner workings of their system and identify potential issues that may impact performance or security.

lsof Syntax:

Terminal window
lsof [options] [parameters]

Options:

OptionDescription
-cSelects the listing of files for processes executing the command cmd.
-iSelects the listing of files any of whose address ranges overlap the address range intended for the listing’s address space.
-nInhibits the conversion of network numbers to host names for network files.
-pSelects the listing of files for the processes whose process ID’s match that given in the comma-separated set pids.
-uSelects the listing of files for the user whose login names or user ID’s match that given in the comma-separated set user names or user ID’s.
-fCauses lsof to treat the names given as individual file descriptors or open file descriptions, not as pathnames.
-gSelect files whose file or process group ID is in the comma-separated set group IDs.
-tSpecifies the terminal associated with a search entered with the -a, -c, -g, -i, -p, -s, -t, or -u option.
-VWrites configuration information to standard output.
-?Shows help information.

Parameters:

ParameterDescription
[name]File name or process name to filter the results by.
[PID]Process ID number to filter results by.
[UID]User ID number to filter results by.
[process]Process name to filter the results by.
[file]File path to filter the results by.
[protocol]Protocol name to filter the results by.
[IPv4]IPv4 address to filter the results by.
[IPv6]IPv6 address to filter the results by.

lsof Usage:

List all open files

Terminal window
lsof

Displays a list of all open files and the processes that have opened them.

Show open files for a specific process

Terminal window
lsof -p <PID>

Displays a list of all open files for a specific process identified by its process ID (PID).

List all network connections

Terminal window
lsof -i

Shows a list of all network connections and the associated processes.

Find which process is using a specific file

Terminal window
lsof /path/to/file

Identifies which process is currently using a specific file by specifying the file path as an argument.

How do I use lsof in MacOS?

To use the lsof command in MacOS, execute the following command:

Terminal window
lsof -i :8080

How can I list all open files with lsof in MacOS?

To list all open files with lsof in MacOS, use the following command:

Terminal window
lsof

How can I search for processes that are using a specific file with lsof in MacOS?

To search for processes using a specific file with lsof in MacOS, run the command:

Terminal window
lsof /path/to/file

How can I see network connections with lsof in MacOS?

To view network connections with lsof in MacOS, execute the command:

Terminal window
lsof -i

How can I display open files for a specific user with lsof in MacOS?

To show open files for a particular user with lsof in MacOS, use the following command:

Terminal window
lsof -u username

How do I find processes using a specific port with lsof in MacOS?

To find processes using a specific port with lsof in MacOS, run the command:

Terminal window
lsof -i :port_number

How can I list all open files for a specific process ID with lsof in MacOS?

To list all open files for a specific process ID with lsof in MacOS, use the following command:

Terminal window
lsof -p process_id

Applications of the lsof command

  • List open files and processes
  • Identify network connections
  • Investigate which files are being used by a specific process
  • Troubleshoot file permission issues
  • Monitor file system activity