regular file, a directory, a
symbolic link, a special block file, an NFS mounted file, a socket stream, a shared library, a special character file, a regular pipe, a named pipe, an internet socket, a UNIX domain socket, and many others. I have mostly used it to find all the files opened by a particular process, which I will show you in the next section.It's an incredibly useful tool to do debugging and troubleshooting in UNIX and Linux environments. Because of its versatile nature, it's also known as the "The Swiss Army Knife of Linux." Since most of the production system runs on the UNIX-based operating system, knowledge of this tool becomes even more critical.
Java developer, I often use lsof command to find all the files opened my Java process or some time to find which process has opened a particular file, e.g., a file that is used to represent a cache in memory, lsof is incredibly useful on doing that.
In this tutorial, we are going to learn different ways and options to use lsof some tasks, which will further help you to find a process and file-related information in UNIX or Linux. You can combine the lsof with grep command for advanced search and filtering.
I often use grep command to filter output based upon file name, process id, or file type. If you are not familiar with basic commands like grep and find, then I suggest you take a look at
Linux Command Line Basics on Udemy, an essential course for any programmer, tester, data security people, or anyone, who work on UNIX and Linux based environment.
How to get lsof commandThe lsof command generally comes pre-installed in many UNIX systems. If you are getting -bash: lsof: command not found an error while using lsof, then it could be that lsof is not in your
PATH. Just check /usr/bin or /usr/sbin folder for this command. If you don't find it there, then you can install it from the source, or you can ask your UNIX admin to do that for you.
Now, let's see the 10 examples of lsof command which developers can use for troubleshooting in the Linux machine.
1) How to list all open files by all processes in Linux?
$ lsofOnly running lsof without any argument, print all opened file and process. This is not particularly useful but a good starting point.
2) How to list all process which has opened a file in Linux?
$ lsof /home/someuser/somefileWill list all the process which has opened this file. You can see the command, PID, user, and full file path to
findout the process.
3) How to find all opened files by a user in Linux?You can use lsof -u command to list all opened files by a user, as shown below.
$ lsof -u usernameYou can provide a comma-separated list of users to find a list of open files by multiple users, as shown below.
$ lsof -u user1,user2,user3You can do the same by providing -u option multiple times :
$ lsof -u user1 -u user2If you are struggling to remember these lsof command options, here is a helpful diagram from Julia Evans, which will help them to remember. If you want, you can also join the
Learn The Linux Command Line: Basic Commands, a free course on Udemy for learning essential Linux commands for FREE.
4) How to list all files opened by a particular command in UNIX?You can use lsof -c option to provide the name of the command and list down all the files opened by that command, for example, to list all file opened by
java process, you can do this :
$ lsof -c javaThis is better than using
grepfor filtering, as instead of writing lsof | grep java, you can just write lsof -c java.
You can also find all files opened by apache which runs as httpd as shown below :
lsof -c httpdJust like multiple users, you can also combine multiple processes name to list down files hold by them e.g.
$ lsof -c java -c httpd
5) How to find all files opened by a particular user and command in Linux?You can combine users and process name in one lsof command to list down all the files opened by a particular process or a specific user, as shown below :
$ lsof -u root -c javaThis will list all files opened or hold by root user + all files opened by the java process. See
Linux Command Line Interface (CLI) Fundamentals, one of the great courses to learn the Linux command line on Pluralsight.
6) How to find files opened by USER and process in Linux?Like the previous option, you can also combine user and process by using lsof option '-a'. This is like the AND logical operator and will only list files, which matches both options e.g.
$ lsof -a -u root - c javawill only list files opened by java process which is running under the root user
7) lsof with the negation operator exampleSimilar to AND and OR operator used earlier, you can also use negation operator with lsof command like
$ lsof - u ^rootwill list all files opened by all user except root
8) How to list all open files by a process using PID in UNIX?As I told, I mostly use the lsof command to find all files opened by a particular process. To do that sometimes, I usually use grep command to filter lsof output by PID, but you can also use lsof -p option to do the same, as shown below :
$ lsof -p 17783will list all files opened by the process with PID 17783.
List users and processes, you can also supply multiple PIDs to find files opened by multiple processes, e.g. :
$ lsof -p 17783,17754,17984We will list all files opened by the process with PIDs 17783,17754,17984. You can also see the
Practical Guide to Linux Commands, Editors, and Shell Programming 3rd Editionby Mark G. Sobell to learn more about how to find a process in UNIX.
9) How to list all network connections in Linux?You can use lsof - i option to find all open network connections, which are nothing but open internet sockets (
TCP and UDP), for example.
$ lsof -iYou can further find all TPC connection by using the tcp option as shown below :
$ lsof -i tcpSimilarly, to find all open udp connections you can do :
$ lsof -i udpwill list all processes with open internet sockets.
10) How to find which process is using a particular port in Linux?Though you can do this with netstat command as well, you would be surprised to know that you can
find all processes using a particular TCP or UDP portusing the lsof command. For example :
$ lsof -i :19500will find the process which is using TCP or UDP port 19500
You can even names defined in etc/services instead of port number e.g.
$ lsof -i :smtpwill print the process using the SMTP port.
You can also combine
TCP and UDP with a port to do a more specific search, e.g., to find all process in UNIX which uses TCP port number 19600 you can do the following :
$ lsof -i tcp:19600and to find all process which is using UDP port 17600, you can use
$ lsof -i udp:17600
Now that you have seen how to use lsof command to do different things, let's revise them so that you can remember whatever you have learned so far. Here is a nice summary of lsof command examples in Linux:
Bonus TipsYou can use the lsof commands to find all the files that are deleted but still hold the disk space. This is very useful when your disk or filesystem is 100% full, and you are looking for files and directory to delete to free some space. Suppose /app is your file system then.
$ lsof /app | grep deleted
Will print all deleted files which are claiming disk space. You can just kill the process which is holding the reference of those files and get back your disk space. The command will also print the process id to help you kill the process. You can just kill command for that.
That's all about 10 examples of lsof command in UNIX and Linux. As I said, it's incredibly useful to find the list of files opened by a particular processor to find all the process which holds a lock on a file. Since almost everything is a file in UNIX, you can use lsof to find out open socket, directory, symbolic link, internet socket, and many others. You can also see the lsof man page for full documentation and more options.
Linux Command Line Basics
Linux Command Line Interface (CLI) Fundamentals
5 Free Linux Courses for Programmers
5 Courses to Learn Shell Scripting for Developers
10 Tips to improve your speed in Linux command lines
10 Basic Networking Command Every Linux User Should Know
10 Examples of cURL command in Linux
10 Examples of chmod command in UNIX
Thanks for reading this article so far. If you find these lsof commands useful, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.
P. S. - If you are serious about improving your Linux skills, then I also suggest you look at the
Learn Linux in 5 Days and Level Up Your Career course on Udemy. It's a great course for everyone, and you will definitely learn some useful tips to work better on Linux.
My friend circle has a lot of programmers and non-programmers. When it comes to buying
Guys, It's a fun time here, as Holidays are approaching, it's Movie time for many of us. Being a computer programmer, I love movies which are based on programmers life, wor..