{"id":17945,"date":"2022-08-03T18:49:15","date_gmt":"2022-08-03T18:49:15","guid":{"rendered":"https:\/\/linuxways.net\/?p=17945"},"modified":"2022-08-17T04:01:03","modified_gmt":"2022-08-17T04:01:03","slug":"linux-lsof-command","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/linux-lsof-command\/","title":{"rendered":"Linux LSOF Command"},"content":{"rendered":"<p>\u201cIn this Linux tutorial, we will try to explore the usage of a very useful Linux command, i.e., the LSOF command. We will first introduce you to the basic purpose and the general syntax of this command, followed by a few useful examples that will help you a lot in using this command. So, let us get started.\u201d<\/p>\n<h2><strong>Purpose of the LSOF Command in Linux<\/strong><\/h2>\n<p>LSOF literally stands for \u201cList Open Files,\u201d and as its name says, this command is used for listing down all the currently opened files of your system. The general syntax of this command is as follows:<\/p>\n<pre>\r\n$ lsof [option]\r\n<\/pre>\n<p>Here, you can replace \u201coption\u201d with any of the options that are available with this command.<\/p>\n<h2><strong>Usage of the LSOF Command in Linux<\/strong><\/h2>\n<p>To learn how you can use the LSOF command in Linux, you can go through the following examples:<\/p>\n<h3><strong>Example # 1: Listing All the Open Files in Linux<\/strong><\/h3>\n<p>In this example, we will be using the LSOF command in Linux for simply listing down all of the open files. For that, we will run this command in the manner shown below:<\/p>\n<pre>\r\n$ lsof\r\n<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"267\" height=\"22\" class=\"wp-image-17949\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-1.png\" \/><\/strong><\/p>\n<p>Running this command without any options prints the details about all the currently opened files on the terminal, as shown in the following image. This command will take some time for execution as it has to produce a lengthy output.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"647\" height=\"411\" class=\"wp-image-17950\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-2.png 647w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-2-300x191.png 300w\" sizes=\"auto, (max-width: 647px) 100vw, 647px\" \/><\/p>\n<h2><strong>Example # 2: Listing All the Open Files Belonging to a Particular User in Linux<\/strong><\/h2>\n<p>Now, we will use the LSOF command to print the details about all the open files belonging to a particular Linux user since a Linux system can have multiple users at the same time. For that, we will use the command shown below:<\/p>\n<pre>\r\n$ lsof \u2013u system\r\n<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"343\" height=\"23\" class=\"wp-image-17951\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-3.png 343w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-3-300x20.png 300w\" sizes=\"auto, (max-width: 343px) 100vw, 343px\" \/><\/strong><\/p>\n<p>The LSOF command with the &#8220;-u&#8221; flag followed by the username prints all the open files belonging to the specified user. In this example, we wanted to print the open files belonging only to the Linux user named \u201csystem\u201d. You can replace it with the username of the user whose open files you wish to list down.<\/p>\n<p>All the open files belonging to the specified user of our Linux system are shown in the following image:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"646\" height=\"410\" class=\"wp-image-17952\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-4.png 646w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-4-300x190.png 300w\" sizes=\"auto, (max-width: 646px) 100vw, 646px\" \/><\/p>\n<h2><strong>Example # 3: Listing All the Open Files Associated With a Specific Process ID in Linux<\/strong><\/h2>\n<p>In this example, we intend to print all the open files that are associated with a specific process ID. For that, we will use the LSOF command in the manner shown below:<\/p>\n<pre>\r\n$ lsof \u2013p 1129\r\n<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"331\" height=\"22\" class=\"wp-image-17953\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-5.png 331w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-5-300x20.png 300w\" sizes=\"auto, (max-width: 331px) 100vw, 331px\" \/><\/strong><\/p>\n<p>Whenever the LSOF command is paired up with the \u201c-p\u201d flag followed by a particular process ID, then all the open files associated with that particular process ID are printed on the terminal. You can replace \u201c1129\u201d with the process ID of the process whose associated open files you want to print.<\/p>\n<p>All the open files associated with the specified process ID of our Linux system are shown in the following image:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"642\" height=\"412\" class=\"wp-image-17954\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-6.png 642w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-6-300x193.png 300w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/p>\n<h2><strong>Example # 4: Listing All the Open Files Associated With the Parent Processes in Linux<\/strong><\/h2>\n<p>Now, we simply want to list down all those open files that are associated with the parent processes in Linux. For that, we will use the LSOF command as shown below:<\/p>\n<pre>\r\n$ lsof \u2013R\r\n<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"290\" height=\"22\" class=\"wp-image-17955\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-7.png\" \/><\/strong><\/p>\n<p>The \u201c-R\u201d flag is used with the LSOF command for listing all the open files associated only with the parent processes in Linux.<\/p>\n<p>All the open files associated with the parent processes of our Linux system are shown in the following image:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"621\" height=\"410\" class=\"wp-image-17956\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-8.png 621w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-8-300x198.png 300w\" sizes=\"auto, (max-width: 621px) 100vw, 621px\" \/><\/p>\n<h2><strong>Example # 5: Listing All the Open Files Associated With the Network Connections Following a Particular Protocol in Linux<\/strong><\/h2>\n<p>In this example, we wish to list down all the open files associated with the network connections of Linux that follow a particular protocol such as TCP or UDP. For that, we will use the LSOF command in the manner shown below:<\/p>\n<pre>\r\n$ sudo lsof \u2013i tcp\r\n<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"360\" height=\"25\" class=\"wp-image-17957\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-9.png 360w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-9-300x21.png 300w\" sizes=\"auto, (max-width: 360px) 100vw, 360px\" \/><\/strong><\/p>\n<p>This command requires \u201csudo\u201d privileges to execute. Moreover, you can simply replace \u201ctcp\u201d with \u201cudp\u201d if you want to display all the open files associated with this particular protocol.<\/p>\n<p>All the open files of our Linux system belonging to the specified protocol are shown in the following image:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"644\" height=\"413\" class=\"wp-image-17958\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-10.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-10.png 644w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-17945-10-300x192.png 300w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>In this tutorial, we wanted to guide you on the usage of the LSOF command in Linux. For that, we began by talking about the purpose of this command, followed by its general syntax. After that, we discussed a few example use-cases of this command that will help you a lot in the further exploration of this command.<\/p>","protected":false},"excerpt":{"rendered":"<p>LSOF literally stands for \u201cList Open Files,\u201d and as its name says, this command is used for listing down all the currently opened files of your system.<\/p>","protected":false},"author":3,"featured_media":18088,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-17945","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=17945"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17945\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/18088"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=17945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=17945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=17945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}