10 Example Of Lsof Commands Inwards Unix In Addition To Linux
It's been a long fourth dimension since I receive got written anything on UNIX or Linux commands, but today I'll verbalise virtually the lsof command, a utility ascendency every arrangement admin too developers love. The lsof ascendency stands for listing opened upwardly file descriptors too every bit the lift suggests, it is used to respect opened upwardly files past times process. Since almost everything inwards UNIX are file, you lot tin role lsof ascendency to respect an opened upwardly regular file, a directory, a symbolic link, a block special file, an NFS mounted file, a socket stream, a shared library, a grapheme special file, a regular pipe, a named pipe, an meshing socket, a UNIX domain socket, too many others. I receive got mostly used it to respect all the files opened past times a detail process, which I volition present you lot inwards the coming section.
It's an incredibly useful tool to create debugging too troubleshooting inwards UNIX too Linux environment, too because of its versatile nature, it's also known every bit the "The Swiss Army Knife of Linux". Since most of the production arrangement runs on the UNIX-based operating system, cognition of this tool acquire out fifty-fifty to a greater extent than important.
As a Java developer, I oftentimes role lsof command to respect all the files opened my Java process, or or thus fourth dimension to respect which procedure has opened a detail file e.g. a file which is used to correspond a cache inwards memory, lsof is incredibly useful on doing that.
In this tutorial, nosotros are going to acquire dissimilar ways too options to role lsof or thus task, which volition farther aid you lot to respect procedure too file related information inwards UNIX or Linux. You tin combine the lsof amongst grep command for advanced search too filtering.
I oftentimes role grep ascendency to filter output based upon file name, procedure id or file type. If you lot are non familiar amongst basic commands similar grep too find, too thus I advise you lot accept a expect at Linux Command Line Basics on Udemy, a basic course of written report for whatsoever programmer, tester, information safety people or anyone, who function on UNIX too Linux based environment.
1) How to listing all opened upwardly files past times all process
Simply running lsof without whatsoever declaration impress all opened file too process. This is non especially useful but a expert starting point.
2) How to listing all procedure which has opened a file
volition listing all the procedure which has opened this file. you lot tin encounter the command, PID, user, too total file path to find out the process.
3) How to respect all opened files past times a user
You tin role lsof -u ascendency to listing all opened file past times a user every bit shown below
You tin furnish comma separated listing of users to respect a list of opened upwardly files past times multiple users every bit shown below
You tin create the same past times providing -u selection multiple times :
If you lot are struggling to think these lsof ascendency options, hither is a overnice 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 costless course of written report on Udemy for learning essential Linux commands for FREE.
4) How to listing all files opened past times a detail command
You tin role lsof -c selection to furnish the lift of the ascendency too listing downwardly all the files opened past times that command, for example, to listing all file opened past times java process, you lot tin create this :
This is ameliorate than using grep for filtering, every bit instead of writing lsof | grep java, you lot tin only write lsof -c java.
You tin also respect all files opened past times apache which runs as httpd every bit shown below :
Just similar multiple users, you lot tin also combine multiple processes lift to listing downwardly files concur past times them e.g.
5) How to respect all files opened past times a detail user too command
You tin combine users too procedure lift inwards i lsof ascendency to listing downwardly all the files opened past times a detail procedure or a detail user every bit shown below :
This volition listing all files opened or concur past times root user + all files opened past times the coffee process. See TCP too UDP), for example
You tin farther respect all TPC connectedness past times using the tcp selection every bit shown below :
Similarly, to respect all opened upwardly udp connections you lot tin create :
volition listing all procedure amongst opened upwardly meshing sockets.
10) How to respect which procedure is using a port
Though you lot tin create this amongst netstat ascendency every bit well, you lot would hold out surprised to know that you lot tin find all procedure using a detail TCP or UDP port using the lsof command. For instance :
volition respect the procedure which is using TCP or UDP port 19500
You tin fifty-fifty names defined inwards etc/services instead of port disclose e.g.
volition impress procedure using the SMTP port.
You tin also combine TCP too UDP amongst a port to create to a greater extent than specific search e.g. to respect all procedure inwards UNIX which uses TCP port disclose 19600 you lot tin create the next :
too to respect all procedure which is using UDP port 17600 you lot tin use
Now that you lot receive got seen how to role lsof ascendency to create dissimilar things, let's revise them thus that you lot tin think whatever you lot receive got learned thus far. Here is a overnice summary of lsof ascendency examples inwards Linux:
Bonus Tip
You tin role the lsof commands to respect all the files are deleted but even thus concur the disk space. This is real useful when your disk or filesystem is 100% total too you lot are looking for files too directory to delete to costless or thus space. Suppose /app is your file arrangement then
$ lsof /app | grep deleted
volition impress all deleted files which are claiming disk space. You tin only kill the procedure which is belongings the reference of those files too acquire dorsum your disk space. The ascendency volition also impress the procedure id to aid you lot kill the process. You tin only kill ascendency for that.
That's all virtually 10 examples of lsof ascendency inwards UNIX too Linux. As I said, it's incredibly useful to respect the listing of files opened past times a detail processor to respect all the procedure which holds a lock on a file. Since almost everything is a file inwards UNIX, you lot tin role lsof to respect out opened upwardly socket, directory, symbolic link, meshing socket, too many others. You tin also encounter the lsof homo page for total documentation too 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 ascendency lines
10 Basic Networking Command Every Linux User Should Know
10 Examples of cURL ascendency inwards Linux
10 Examples of chmod ascendency inwards UNIX
Thanks for reading this article thus far. If you lot respect these lsof commands useful too thus delight part amongst your friends too colleagues. If you lot receive got whatsoever questions or feedback too thus delight drib a note.
P. S. - If you lot are serious virtually improving your Linux skills too thus I also advise you lot expect at the Learn Linux inwards five Days too Level Up Your Career course on Udemy. It's a bully course of written report for everyone too you lot volition definitely acquire or thus useful tips to function ameliorate inwards Linux.
It's an incredibly useful tool to create debugging too troubleshooting inwards UNIX too Linux environment, too because of its versatile nature, it's also known every bit the "The Swiss Army Knife of Linux". Since most of the production arrangement runs on the UNIX-based operating system, cognition of this tool acquire out fifty-fifty to a greater extent than important.
As a Java developer, I oftentimes role lsof command to respect all the files opened my Java process, or or thus fourth dimension to respect which procedure has opened a detail file e.g. a file which is used to correspond a cache inwards memory, lsof is incredibly useful on doing that.
In this tutorial, nosotros are going to acquire dissimilar ways too options to role lsof or thus task, which volition farther aid you lot to respect procedure too file related information inwards UNIX or Linux. You tin combine the lsof amongst grep command for advanced search too filtering.
I oftentimes role grep ascendency to filter output based upon file name, procedure id or file type. If you lot are non familiar amongst basic commands similar grep too find, too thus I advise you lot accept a expect at Linux Command Line Basics on Udemy, a basic course of written report for whatsoever programmer, tester, information safety people or anyone, who function on UNIX too Linux based environment.
How to acquire lsof command
The lsof ascendency mostly comes pre-installed inwards many UNIX systems. If you lot are getting -bash: lsof: ascendency non found an fault piece using lsof too thus it could hold out that lsof is non inwards your PATH. Just banking concern gibe /usr/bin or /usr/sbin folder for this command. If you lot don't respect in that place too thus you lot tin install it from source or you lot tin inquire your UNIX admin to create that for you.1) How to listing all opened upwardly files past times all process
$ lsof
Simply running lsof without whatsoever declaration impress all opened file too process. This is non especially useful but a expert starting point.
2) How to listing all procedure which has opened a file
$ lsof /home/someuser/somefile volition listing all the procedure which has opened this file. you lot tin encounter the command, PID, user, too total file path to find out the process.
3) How to respect all opened files past times a user
You tin role lsof -u ascendency to listing all opened file past times a user every bit shown below
$ lsof -u username You tin furnish comma separated listing of users to respect a list of opened upwardly files past times multiple users every bit shown below
$ lsof -u user1,user2,user3 You tin create the same past times providing -u selection multiple times :
$ lsof -u user1 -u user2
If you lot are struggling to think these lsof ascendency options, hither is a overnice 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 costless course of written report on Udemy for learning essential Linux commands for FREE.
4) How to listing all files opened past times a detail command
You tin role lsof -c selection to furnish the lift of the ascendency too listing downwardly all the files opened past times that command, for example, to listing all file opened past times java process, you lot tin create this :
$ lsof -c java This is ameliorate than using grep for filtering, every bit instead of writing lsof | grep java, you lot tin only write lsof -c java.
You tin also respect all files opened past times apache which runs as httpd every bit shown below :
lsof -c httpd Just similar multiple users, you lot tin also combine multiple processes lift to listing downwardly files concur past times them e.g.
$ lsof -c coffee -c httpd
5) How to respect all files opened past times a detail user too command
You tin combine users too procedure lift inwards i lsof ascendency to listing downwardly all the files opened past times a detail procedure or a detail user every bit 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 too UDP), for example
$ lsof -i
You tin farther respect all TPC connectedness past times using the tcp selection every bit shown below :
$ lsof -i tcp
Similarly, to respect all opened upwardly udp connections you lot tin create :
$ lsof -i udp
volition listing all procedure amongst opened upwardly meshing sockets.
10) How to respect which procedure is using a port
Though you lot tin create this amongst netstat ascendency every bit well, you lot would hold out surprised to know that you lot tin find all procedure using a detail TCP or UDP port using the lsof command. For instance :
$ lsof -i :19500
volition respect the procedure which is using TCP or UDP port 19500
You tin fifty-fifty names defined inwards etc/services instead of port disclose e.g.
$ lsof -i :smtp
volition impress procedure using the SMTP port.
You tin also combine TCP too UDP amongst a port to create to a greater extent than specific search e.g. to respect all procedure inwards UNIX which uses TCP port disclose 19600 you lot tin create the next :
$ lsof -i tcp:19600
too to respect all procedure which is using UDP port 17600 you lot tin use
$ lsof -i udp:17600
Now that you lot receive got seen how to role lsof ascendency to create dissimilar things, let's revise them thus that you lot tin think whatever you lot receive got learned thus far. Here is a overnice summary of lsof ascendency examples inwards Linux:
Bonus Tip
You tin role the lsof commands to respect all the files are deleted but even thus concur the disk space. This is real useful when your disk or filesystem is 100% total too you lot are looking for files too directory to delete to costless or thus space. Suppose /app is your file arrangement then
$ lsof /app | grep deleted
volition impress all deleted files which are claiming disk space. You tin only kill the procedure which is belongings the reference of those files too acquire dorsum your disk space. The ascendency volition also impress the procedure id to aid you lot kill the process. You tin only kill ascendency for that.
That's all virtually 10 examples of lsof ascendency inwards UNIX too Linux. As I said, it's incredibly useful to respect the listing of files opened past times a detail processor to respect all the procedure which holds a lock on a file. Since almost everything is a file inwards UNIX, you lot tin role lsof to respect out opened upwardly socket, directory, symbolic link, meshing socket, too many others. You tin also encounter the lsof homo page for total documentation too 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 ascendency lines
10 Basic Networking Command Every Linux User Should Know
10 Examples of cURL ascendency inwards Linux
10 Examples of chmod ascendency inwards UNIX
Thanks for reading this article thus far. If you lot respect these lsof commands useful too thus delight part amongst your friends too colleagues. If you lot receive got whatsoever questions or feedback too thus delight drib a note.
P. S. - If you lot are serious virtually improving your Linux skills too thus I also advise you lot expect at the Learn Linux inwards five Days too Level Up Your Career course on Udemy. It's a bully course of written report for everyone too you lot volition definitely acquire or thus useful tips to function ameliorate inwards Linux.


0 Response to "10 Example Of Lsof Commands Inwards Unix In Addition To Linux"
Post a Comment