TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A Unix Utility to Know About: lsof (2009)

149 pointsby kerckerover 8 years ago

11 comments

nailerover 8 years ago
For finding what uses a file, I&#x27;m an &#x27;fuser&#x27; kid but lsof is fine too. Using posh right now, so had to make my own:<p><pre><code> function fuser($relativeFile){ $file = Resolve-Path $relativeFile foreach ( $Process in (Get-Process)) { foreach ( $Module in $Process.Modules) { if ( $Module.FileName -like &quot;$file*&quot; ) { $Process | select id, path } } } } </code></pre> In use:<p><pre><code> &gt; fuser .\node_modules\ Id Path -- ---- 2660 C:\Program Files\nodejs\node.exe </code></pre> Since it&#x27;s object pipelining, you can:<p><pre><code> &gt; fuser .\node_modules\ | kill</code></pre>
评论 #12366019 未加载
voidzover 8 years ago
`lsof` and `ps` have the most dense man pages I have ever tried to plow through, and that&#x27;s causing me to use these tools only at a minimum.
评论 #12364918 未加载
评论 #12364879 未加载
评论 #12369137 未加载
corfordover 8 years ago
<i>lsof -i -n -P</i> is a regular &quot;reflex&quot; command whenever I&#x27;m trying to figure out why a daemon I&#x27;ve just setup isn&#x27;t responding to requests (immediately answers the questions: is it running? and, if yes, under what privs? and on what interface?).<p>That together with a dump of active iptables rules normally results in an immediate fix for 90% of &quot;why can&#x27;t I connect to X&quot; problems :)
评论 #12368037 未加载
rcontiover 8 years ago
Most days, HN threads make me feel really dumb. On rare days, they make me feel really smart.
newscrackerover 8 years ago
Although a bit rare in my usage, I have found basic use of lsof quite useful when needed. I haven&#x27;t even tried many command line options. The options it provides really seem comprehensive. I have also found similar tools on Windows (like WhoLockMe or Process Explorer) quite useful.
评论 #12365540 未加载
ameliusover 8 years ago
What kernel functions does this command invoke to retrieve this information? How could I write my own version of lsof?
评论 #12365051 未加载
评论 #12364925 未加载
评论 #12365140 未加载
评论 #12365687 未加载
_c_over 8 years ago
s&#x2F;Unix&#x2F;Linux&#x2F;<p>The following can be adapted to provide other information: whatever procfs provides. This is a rough equivalent of &quot;pgrep -fl .|less&quot;. <i>Work-in-progress</i>. Don&#x27;t know if Linux grep has &quot;-a&quot; option.<p><pre><code> #! &#x2F;bin&#x2F;sh # Almquist clone, not Bash case $# in 0) exec grep -a . proc&#x2F;[0-9]*&#x2F;cmdline \ |exec tr &#x27;\000&#x27; &#x27;\040&#x27; \ |exec sed &#x27; &#x2F;grep -a .* proc&#x2F;d; #parent: &#x27;&quot;$$&quot;&#x27;; s&#x2F;proc.&#x2F; &#x2F;; s&#x2F;\&#x2F;cmdline:&#x2F; &#x2F;; &#x27; \ |exec less ;; *) exec grep -a . proc&#x2F;[0-9]*&#x2F;cmdline \ |exec tr &#x27;\000&#x27; &#x27;\040&#x27; \ |exec grep $@ \ |exec sed &#x27; &#x2F;grep &#x27;&quot;$@&quot;&#x27;&#x2F;d; s&#x2F;proc.&#x2F; &#x2F;; s&#x2F;\&#x2F;cmdline:&#x2F; &#x2F;; &#x27; \ |exec less esac</code></pre>
评论 #12367402 未加载
amyjessover 8 years ago
lsof is my go-to command for &quot;why won&#x27;t this drive unmount?&quot;. That alone makes it incredibly useful.<p>Also, back in the bad old days, &#x27;lsof | grep snd&#x27; helped track down what the hell was hogging my sound card (setting up proper mixing has made that a distant memory, though).
评论 #12367552 未加载
0x0over 8 years ago
You can save quite some time waiting for reverse dns lookups if you use &quot;lsof -n&quot;
sreeramvenkatover 8 years ago
I frequently use lsof to get the location of a log file created by a process.<p>something like,<p>lsof | grep &lt;pid&gt; | grep log
gkyaover 8 years ago
fstat(1) is similar on FreeBSD, lsof is in the ports.