Linux lsof command explained with 12 practical examples

The lsof is an acronym for List of open files that displays detailed info on which files are held open on a Linux system and which processes have opened them. It was developed and supported by Victor A. Abell.

This article will help you to understand the lsof command usage along with 12 practical examples.

All Open Files List using lsof

To view the list of the open files simply execute lsof and you will get the output like below where you can see a header like a command, Pid, User, FD, etc.

$ lsof

List open files with lsof

In the above example, most of the columns and their values are self-explanatory. So let’s see what actually FD is, Fd refers to File Descriptor and it contains values like:

  • cwd- current working directory
  • rtd – root directory
  • txt – text
  • mem – memory-mapped file

Open Files List for Specific User

You can specify the user using the -u option to the command which lets you list all the open files for that user.

$ lsof -u root

Open files by user

List Open File By Process Id

Suppose you know the pid of the specific process you can search the open file list based on pid using -p option along with the command trailing with pid value. You need to run the command as specified in the example given below.

# lsof -p 82

List files by pid

Specific Port Running Process

To check which process occupied the specific port you can list them using the -i option along with the port number at TCP/UDP. In the following example, let’s find which process has taken port 80.

# lsof -i:80

Find processes that listen on specific port

Display Specific Network Files (IPv4 & IPv6) List

In order to display the open files according to network files type, you need to specify the type trailing the -i option.

For IPv4 you need to run the command like the below.

# lsof -i 4

List network connections

For, IPv6 run the command,

# lsof -i 6

IPv6 connections

Find processes that listen to a specific port range

If you want to display the list of the open files of the specific port range. You need to use -i trailing the command with a specific port range.

# losf -i TCP:1-100

network connections of specific port range

Exclude the Specific User from the Open Files List

You can exclude the users from the output using the ‘^’ character to the command as given in the following example.

# lsof -i -u^sanju

Exclude parameter

List processes by directory path

To find a process running on a specific directory you can run a command with +D option trailing the path along with it. You have to execute the command in the following way.

# lsof +D /home/sanju

Find processes by directory path

Display Network Connections

User -i option to the command in order to view the list of the network connection based on Listening & Established.

$ lsof -i

Kill Specific Process

Sometimes some processes may resist shutdown even if you close the process or some unknown process may take up the port where you want to run a specific program. In order to force the shutdown of the process, you can use the lsof command as it’s easier to list the running process. So you need to run the command in the following way.

# kill -9 $(lsof -t -i:8080)

List Open Files Based on FD (File Descriptor)

You can filter the open files based on FD types, you need to use -d option along with FD type as mentioned in the example below.

# lsof -d rtd

Open files by file descriptor

List Open Files Based On Process Name

You can list the files according to the process name using the -c option with the command. Run the command in the following ways

# lsof -c nginx

Find processes by name

Conclusion

I am grateful to you for giving your time to go through this article. I hope you gained some knowledge on how lsof command can be used. These are the things that I find useful during deploying the application.