How to use lsof command in Linux? Example Tutorial

Hello guys, If you want to learn how to use lsof command in Linux then you have come to the right place. In the last few article, I talk about find, grep, mailx, crontable and du command in Linux and in this article I am going to talk about another useful Linux command, the lsof command. The lsof  command lists open files and that's why its named lsof. This command is very important for server side developer and IT support people who need to check which file is held by which process and find the deleted files which are hogging memory etc. It act as a useful debugging and troubleshooting tool in Linux.

The lsof command reveals the files which are opened by a process. In Linux/Unix, everything is considered as a file such as pipes, sockets, devices, etc are treated as files. Thus, by using the ‘lsof’ command we will get the information of any opened files.

10 ways to use lsof command in Linux

I like to learn from seeing examples and that's why I am going to share 10 common examples of lsof command in Linux in this article and you will learn how to use lsof along the way.

1, List all open files using the lsof command

$ lsof

This command long lists all the open files. In the screenshot, we can see some columns such as command, PID, user, FD, TYPE, etc

How to use lsof command in Linux? Example Tutorial

FD - File descriptor
Here are common values of file descriptors are:
  • cwd - current working directory
  • txt - Text file
  • mem - Memory Mapped file
  • mmap - Memory Mapped device
  • Num represents the actual file descriptor. For example, 0u, 1w, and 3r where r represents read, w represents write and u represents both read and write.

Type represents the type of the file. Some of the values of TYPE are
  • REG - Regular file
  • DIR - Directory
  • CHR - Character special file
  • FIFO - First in first out

2, How to list user-specific opened files

$ lsof -u user_name

3. How to find and list the process running on a port

$ lsof -i :port_number

4. How to Lists only IPv4 and IPv6 open files

$ lsof -i 4  - For IPv4
$ lsof -i 6  - For IPv6

5. How to Lists open files for TCP port ranges 1-1024

$ lsof -i :1-1024

6. How to list process Lists by pid

$ lsof -p PID

7. How to Kill all activity for a particular user

$ killall -9 `lsof -t -u username`

8. How to list all opened files in a directory

$ lsof +D path_of_the_directory

9. How to list all Opened files based on process names using lsof

$ lsof -c process_name

10. How to list all network connections using lsof command

$ lsof -i

That's all about how to use the lsof command in UNIX and Linux. You can see you can do a lot of things using lsof command like you can list all network connection, you can find all opened files based upon process name, you can find deleted files which are still hogging memory because a process has their reference. I really like this command and I think every developer, IT support, and system admin should know this command by heart, it will help you immensely on Linux server during troubleshooting. 

Other Linux Articles and Resources you may like
  • How to set up cron jobs in Linux (Crontab example)
  • 5 Example of kill commands in Unix and Linux (example)
  • 10 examples of Networking commands in Unix (nslookup)
  • Top 5 Courses to learn Vim Editor (online courses)
  • 10 Linux command line courses for Beginners (courses)
  • VI Editor examples and tips for beginners (vi examples)
  • How does the nslookup command work in UNIX? (answer)
  • 7 Best Linux Courses for DevOps Engineers (Linux courses)
  • 10 examples of lsof command in Linux? (examples)
  • Linux find + du + grep example (example)
  • How to use the netstat command to find which process is listening on a port? (example)
  • 10 Examples of curl command in Linux (cURL)
  • 10 Examples of chmod command in Linux (chmod)

Thanks for reading this article so far. If you like these Linux tutorial about using lsof command and my examples  then please share it with your friends and colleagues.

P. S. - If you are looking for some free online courses to start your Linux journey, you should check out my list of Free Linux Courses for Programmers, Software Developers, IT support, IT Professionals, and System Administrators.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.