It's been a long fourth dimension since I conduct maintain written anything on UNIX or Linux commands. Still, today, I'll beak virtually the lsof command, a utility ascendance every organisation admin together with developers love. The lsof ascendance stands for listing opened upward file descriptors, together with equally the get upward suggests, it is used to detect opened upward files past times the process. Since almost everything inwards UNIX are file, you lot tin purpose lsof ascendance to detect an opened upward regular file, a directory, a symbolic link, a special block file, an NFS mounted file, a socket stream, a shared library, a special graphic symbol file, a regular pipe, a named pipe, an cyberspace socket, a UNIX domain socket, together with many others. I conduct maintain mostly used it to detect all the files opened past times a detail process, which I volition demo you lot inwards the side past times side section.
It's an incredibly useful tool to produce debugging together with troubleshooting inwards UNIX together with Linux environments. Because of its versatile nature, it's also known equally the "The Swiss Army Knife of Linux." Since most of the production organisation runs on the UNIX-based operating system, cognition of this tool becomes fifty-fifty to a greater extent than critical.
As a Java developer, I frequently purpose lsof command to detect all the files opened my Java procedure or but about fourth dimension to detect which procedure has opened a detail file, e.g., a file that is used to correspond a cache inwards memory, lsof is incredibly useful on doing that.
In this tutorial, nosotros are going to larn dissimilar ways together with options to purpose lsof but about tasks, which volition farther aid you lot to detect a procedure together with file-related information inwards UNIX or Linux. You tin combine the lsof amongst grep command for advanced search together with filtering.
I frequently purpose grep ascendance to filter output based upon file name, procedure id, or file type. If you lot are non familiar amongst basic commands similar grep together with find, together with thus I advise you lot accept a expect at Linux Command Line Basics on Udemy, an essential course of report for whatsoever programmer, tester, information safety people, or anyone, who operate on UNIX together with Linux based environment.
Now, let's run across the 10 examples of lsof ascendance which developers tin purpose for troubleshooting inwards the Linux machine.
Only running lsof without whatsoever argument, impress all opened file together with process. This is non especially useful but a adept starting point.
Will listing all the procedure which has opened this file. You tin run across the command, PID, user, together with total file path to find out the process.
You tin supply a comma-separated listing of users to detect a list of opened upward files past times multiple users, equally shown below.
You tin produce the same past times providing -u pick multiple times :
If you lot are struggling to recall these lsof ascendance options, hither is a helpful diagram from Julia Evans, which volition aid them to remember. If you lot want, you lot tin also bring together the Learn The Linux Command Line: Basic Commands, a complimentary course of report on Udemy for learning essential Linux commands for FREE.
This is improve than using grep for filtering, equally instead of writing lsof | grep java, you lot tin but write lsof -c java.
You tin also detect all files opened past times apache which runs as httpd equally shown below :
Just similar multiple users, you lot tin also combine multiple processes get upward to listing downwards files concur past times them e.g.
This volition listing all files opened or concur past times root user + all files opened past times the coffee process. See TCP together with UDP), for example.
You tin farther detect all TPC connectedness past times using the tcp pick equally shown below :
Similarly, to detect all opened upward udp connections you lot tin produce :
volition listing all processes amongst opened upward cyberspace sockets.
volition detect the procedure which is using TCP or UDP port 19500
You tin fifty-fifty names defined inwards etc/services instead of port expose e.g.
volition impress the procedure using the SMTP port.
You tin also combine TCP together with UDP amongst a port to produce a to a greater extent than specific search, e.g., to detect all procedure inwards UNIX which uses TCP port expose 19600 you lot tin produce the next :
together with to detect all procedure which is using UDP port 17600, you lot tin use
Now that you lot conduct maintain seen how to purpose lsof ascendance to produce dissimilar things, let's revise them thus that you lot tin recall whatever you lot conduct maintain learned thus far. Here is a prissy summary of lsof ascendance examples inwards Linux:
$ lsof /app | grep deleted
Will impress all deleted files which are claiming disk space. You tin but kill the procedure which is asset the reference of those files together with acquire dorsum your disk space. The ascendance volition also impress the procedure id to aid you lot kill the process. You tin but kill ascendance for that.
That's all virtually 10 examples of lsof ascendance inwards UNIX together with Linux. As I said, it's incredibly useful to detect the listing of files opened past times a detail processor to detect all the procedure which holds a lock on a file. Since almost everything is a file inwards UNIX, you lot tin purpose lsof to detect out opened upward socket, directory, symbolic link, cyberspace socket, together with many others. You tin also run across the lsof human page for total documentation together with to a greater extent than options.
Further Learning
Linux Command Line Basics
5 Free Linux Courses for Programmers
5 Courses to Learn Shell Scripting for Developers
10 Tips to improve your speed inwards Linux ascendance lines
10 Basic Networking Command Every Linux User Should Know
10 Examples of cURL ascendance inwards Linux
10 Examples of chmod ascendance inwards UNIX
Thanks for reading this article thus far. If you lot detect these lsof commands useful, together with thus delight part it amongst your friends together with colleagues. If you lot conduct maintain whatsoever questions or feedback, together with thus delight drib a note.
P. S. - If you lot are serious virtually improving your Linux skills, together with thus I also advise you lot expect at the Learn Linux inwards v Days together with Level Up Your Career course on Udemy. It's a dandy course of report for everyone, together with you lot volition definitely larn but about useful tips to operate improve on Linux.
It's an incredibly useful tool to produce debugging together with troubleshooting inwards UNIX together with Linux environments. Because of its versatile nature, it's also known equally the "The Swiss Army Knife of Linux." Since most of the production organisation runs on the UNIX-based operating system, cognition of this tool becomes fifty-fifty to a greater extent than critical.
As a Java developer, I frequently purpose lsof command to detect all the files opened my Java procedure or but about fourth dimension to detect which procedure has opened a detail file, e.g., a file that is used to correspond a cache inwards memory, lsof is incredibly useful on doing that.
In this tutorial, nosotros are going to larn dissimilar ways together with options to purpose lsof but about tasks, which volition farther aid you lot to detect a procedure together with file-related information inwards UNIX or Linux. You tin combine the lsof amongst grep command for advanced search together with filtering.
I frequently purpose grep ascendance to filter output based upon file name, procedure id, or file type. If you lot are non familiar amongst basic commands similar grep together with find, together with thus I advise you lot accept a expect at Linux Command Line Basics on Udemy, an essential course of report for whatsoever programmer, tester, information safety people, or anyone, who operate on UNIX together with Linux based environment.
How to acquire lsof command
The lsof ascendance by together with large comes pre-installed inwards many UNIX systems. If you lot are getting -bash: lsof: ascendance non found an mistake spell using lsof, together with thus it could hold upward that lsof is non inwards your PATH. Just cheque /usr/bin or /usr/sbin folder for this command. If you lot don't detect it there, together with thus you lot tin install it from the source, or you lot tin inquire your UNIX admin to produce that for you.Now, let's run across the 10 examples of lsof ascendance which developers tin purpose for troubleshooting inwards the Linux machine.
1) How to listing all opened upward files past times all processes inwards Linux?
$ lsof
Only running lsof without whatsoever argument, impress all opened file together with process. This is non especially useful but a adept starting point.
2) How to listing all procedure which has opened a file inwards Linux?
$ lsof /home/someuser/somefile
Will listing all the procedure which has opened this file. You tin run across the command, PID, user, together with total file path to find out the process.
3) How to detect all opened files past times a user inwards Linux?
You tin purpose lsof -u ascendance to listing all opened files past times a user, equally shown below.$ lsof -u username
You tin supply a comma-separated listing of users to detect a list of opened upward files past times multiple users, equally shown below.
$ lsof -u user1,user2,user3
You tin produce the same past times providing -u pick multiple times :
$ lsof -u user1 -u user2
If you lot are struggling to recall these lsof ascendance options, hither is a helpful diagram from Julia Evans, which volition aid them to remember. If you lot want, you lot tin also bring together the Learn The Linux Command Line: Basic Commands, a complimentary course of report on Udemy for learning essential Linux commands for FREE.
4) How to listing all files opened past times a detail ascendance inwards UNIX?
You tin purpose lsof -c pick to supply the get upward of the ascendance together with listing downwards all the files opened past times that command, for example, to listing all file opened past times java process, you lot tin produce this :$ lsof -c java
This is improve than using grep for filtering, equally instead of writing lsof | grep java, you lot tin but write lsof -c java.
You tin also detect all files opened past times apache which runs as httpd equally shown below :
lsof -c httpd
Just similar multiple users, you lot tin also combine multiple processes get upward to listing downwards files concur past times them e.g.
$ lsof -c coffee -c httpd
5) How to detect all files opened past times a detail user together with ascendance inwards Linux?
You tin combine users together with procedure get upward inwards i lsof ascendance to listing downwards all the files opened past times a detail procedure or a specific user, equally shown below :$ lsof -u root -c java
This volition listing all files opened or concur past times root user + all files opened past times the coffee process. See TCP together with UDP), for example.
$ lsof -i
You tin farther detect all TPC connectedness past times using the tcp pick equally shown below :
$ lsof -i tcp
Similarly, to detect all opened upward udp connections you lot tin produce :
$ lsof -i udp
volition listing all processes amongst opened upward cyberspace sockets.
10) How to detect which procedure is using a detail port inwards Linux?
Though you lot tin produce this amongst netstat ascendance equally well, you lot would hold upward surprised to know that you lot tin find all processes using a detail TCP or UDP port using the lsof command. For instance :$ lsof -i :19500
volition detect the procedure which is using TCP or UDP port 19500
You tin fifty-fifty names defined inwards etc/services instead of port expose e.g.
$ lsof -i :smtp
volition impress the procedure using the SMTP port.
You tin also combine TCP together with UDP amongst a port to produce a to a greater extent than specific search, e.g., to detect all procedure inwards UNIX which uses TCP port expose 19600 you lot tin produce the next :
$ lsof -i tcp:19600
together with to detect all procedure which is using UDP port 17600, you lot tin use
$ lsof -i udp:17600
Now that you lot conduct maintain seen how to purpose lsof ascendance to produce dissimilar things, let's revise them thus that you lot tin recall whatever you lot conduct maintain learned thus far. Here is a prissy summary of lsof ascendance examples inwards Linux:
Bonus Tips
You tin purpose the lsof commands to detect all the files that are deleted but yet concur the disk space. This is really useful when your disk or filesystem is 100% full, together with you lot are looking for files together with directory to delete to complimentary but about space. Suppose /app is your file organisation then.$ lsof /app | grep deleted
Will impress all deleted files which are claiming disk space. You tin but kill the procedure which is asset the reference of those files together with acquire dorsum your disk space. The ascendance volition also impress the procedure id to aid you lot kill the process. You tin but kill ascendance for that.
That's all virtually 10 examples of lsof ascendance inwards UNIX together with Linux. As I said, it's incredibly useful to detect the listing of files opened past times a detail processor to detect all the procedure which holds a lock on a file. Since almost everything is a file inwards UNIX, you lot tin purpose lsof to detect out opened upward socket, directory, symbolic link, cyberspace socket, together with many others. You tin also run across the lsof human page for total documentation together with to a greater extent than options.
Further Learning
Linux Command Line Basics
5 Free Linux Courses for Programmers
5 Courses to Learn Shell Scripting for Developers
10 Tips to improve your speed inwards Linux ascendance lines
10 Basic Networking Command Every Linux User Should Know
10 Examples of cURL ascendance inwards Linux
10 Examples of chmod ascendance inwards UNIX
Thanks for reading this article thus far. If you lot detect these lsof commands useful, together with thus delight part it amongst your friends together with colleagues. If you lot conduct maintain whatsoever questions or feedback, together with thus delight drib a note.
P. S. - If you lot are serious virtually improving your Linux skills, together with thus I also advise you lot expect at the Learn Linux inwards v Days together with Level Up Your Career course on Udemy. It's a dandy course of report for everyone, together with you lot volition definitely larn but about useful tips to operate improve on Linux.