{"id":2156,"date":"2015-05-16T23:45:39","date_gmt":"2015-05-16T15:45:39","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2156"},"modified":"2015-05-16T23:45:39","modified_gmt":"2015-05-16T15:45:39","slug":"linux%e7%9a%84lsof%e5%91%bd%e4%bb%a4%e5%ad%a6%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2156.html","title":{"rendered":"Linux\u7684lsof\u547d\u4ee4\u5b66\u4e60"},"content":{"rendered":"<p><strong>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/strong><\/p>\n<ul>\n<li>linux lsof search by filename<\/li>\n<\/ul>\n<p><strong>\u53c2\u8003\u5185\u5bb9\uff1a<\/strong><\/p>\n<p>\u5f88\u65e9\u4e4b\u524d\u6211\u5c31\u542c\u8bf4\u8fc7lsof\u547d\u4ee4\uff0c\u4e5f\u5c1d\u8bd5\u7740\u53bb\u5b66\u4e60\u4f7f\u7528\uff0c\u4f46\u8bb0\u5f97\u5728\u78b0\u5230\u4e86\u51e0\u4e2a\u6ca1\u6cd5\u89e3\u91ca\u7684\u95ee\u9898\u4e4b\u540e\u6211\u5c31\u653e\u5f03\u4e86\u5b66\u4e60\uff0c\u73b0\u5728\u56e0\u4e3a\u53c8\u78b0\u5230\u4e86\uff08\u53d1\u73b0\u5728StackOverflow\u4e0a\u4e5f\u6709\u4eba\u78b0\u5230\u4e86\u548c\u6211\u4e4b\u524d\u78b0\u5230\u7684\u7c7b\u4f3c\u7684\u95ee\u9898\uff09\uff0c\u5c31\u518d\u770b\u770b\uff1a<\/p>\n<blockquote><p>I&#8217;m using linux mint 13 xfce and I have a file named named wv.gold that I&#8217;m trying to check in bash if it&#8217;s open by any program (for instance, I opened it in sublime-text and gedit)<\/p>\n<p>In many forums people say that if I run lsof | grep filename I should get 0 if it&#8217;s open or 256(1) if it&#8217;s closed, but in fact I get nothing (empty string) if I run using grep &#8220;wv.gold&#8221;, and get a little list if I do it using grep gold.<\/p>\n<p>The list is something like:<\/p><\/blockquote>\n<pre class=\"lang:default decode:true\">bash       2045  user   cwd   DIR   8,1     4096     658031 \/home\/user\/path\/to\/dir\nbash       2082  user   cwd   DIR   8,1     4096     658031 \/home\/user\/path\/to\/dir\nwatch      4463  user   cwd   DIR   8,1     4096     658031 \/home\/user\/path\/to\/dir\ngedit     16679  user   cwd   DIR   8,1     4096     658031 \/home\/user\/path\/to\/dir\nlsof      20823  user   cwd   DIR   8,1     4096     658031 \/home\/user\/path\/to\/dir\ngrep      20824  user   cwd   DIR   8,1     4096     658031 \/home\/user\/path\/to\/dir\nlsof      20825  user   cwd   DIR   8,1     4096     658031 \/home\/user\/path\/to\/dir<\/pre>\n<blockquote><p>Thus, I get the path to the directory it is but NOT the path to the file (there are other files there) and either way only to gedit process, not to sublime-text process.\uff08\u53ef\u4ee5\u770b\u5230gedit\uff0c\u4f46\u770b\u4e0d\u5230sublime-text\u8fdb\u7a0b\uff09<\/p>\n<p><span style=\"color: #ff0000;\">Is there some easy way to see if a txt file is opened by any other program?<\/span><\/p>\n<p><strong>EDIT<\/strong>: <span style=\"color: #ff0000;\"><strong>It turns out (cf. comments from @mata and @ctn) that some editors load files and close them immediately, and they just reopen the file when saving it.<\/strong><\/span> This way, we can only see it when they are still opening a big file (since you have the time to observe it while opening) and it disappears immediately after that.\uff08\u56e0\u4e3a\u67d0\u4e9b\u7f16\u8f91\u5668\u5728\u52a0\u8f7d\u4e86\u6587\u4ef6\u4e4b\u540e\u7acb\u5373\u5173\u95edfd\uff0c\u5f53\u9700\u8981\u4fdd\u5b58\u7684\u65f6\u5019\u624d\u4f1a\u518d\u6b21\u6253\u5f00\u3002\u56e0\u6b64\u6211\u4eec\u53ea\u6709\u5728\u6253\u5f00\u975e\u5e38\u5927\u7684\u6587\u4ef6\u65f6\u624d\u4f1a\u770b\u5230\u5bf9\u5e94\u7684fd\uff0c\u5728\u6253\u5f00\u4e86\u4e4b\u540e\u5c31\u770b\u4e0d\u5230\u4e86\u3002\uff09<\/p><\/blockquote>\n<p>==<\/p>\n<p>\u53e6\u5916\u4e00\u4e2a\u67e5\u770b\u65b9\u5f0f\u5c31\u662f\u2014\u2014\u5df2\u77e5\u8fdb\u7a0bID\uff0c\u7136\u540e<span style=\"color: #ff0000;\"><strong>\u901a\u8fc7\/proc\u6587\u4ef6\u7cfb\u7edf<\/strong><\/span>\u8fdb\u884c\u67e5\u770b\uff1a<\/p>\n<ul>\n<li>ls -l \/proc\/&lt;pid&gt;\/fd<\/li>\n<li>ls -l \/proc\/&lt;pid&gt;\/fdinfo<\/li>\n<\/ul>\n<p>==<\/p>\n<pre class=\"lang:default decode:true\">$ lsof \/path\/to\/filename\n$ lsof | grep filename\n\n$ lsof -p &lt;PID&gt;\n\n$ lsof -u&lt;login&gt;\nor\n$ lsof -u&lt;User ID number&gt;\n\n$ lsof -u ^root\nor\n$ lsof -u ^0<\/pre>\n<p>==<\/p>\n<pre class=\"lang:default decode:true\">#!\/bin\/bash\nlsof &lt;file_system_name&gt; &gt; \/dev\/null 2&gt;&amp;1\nif test $? -eq 0; then\n  echo \"&lt;file_system_name&gt; may have no users.\"\nelse\n  echo \"&lt;file_system_name&gt; has some users.\"\nfi<\/pre>\n<p>==<\/p>\n<p><strong>\u53c2\u8003\u94fe\u63a5\uff1a<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/17321930\/check-if-file-is-open-with-lsof\">http:\/\/stackoverflow.com\/questions\/17321930\/check-if-file-is-open-with-lsof <\/a>\u00a0#\u89e3\u60d1<\/li>\n<li><a href=\"http:\/\/linux.die.net\/man\/8\/lsof\">http:\/\/linux.die.net\/man\/8\/lsof <\/a>\u00a0#\u624b\u518c<\/li>\n<li><a href=\"http:\/\/www.akadia.com\/services\/lsof_quickstart.txt\">http:\/\/www.akadia.com\/services\/lsof_quickstart.txt<\/a><\/li>\n<li><a href=\"http:\/\/unix.stackexchange.com\/questions\/60422\/how-to-interpret-this-output-of-lsof-command\">http:\/\/unix.stackexchange.com\/questions\/60422\/how-to-interpret-this-output-of-lsof-command <\/a>\u00a0#\u5bf9lsof\u547d\u4ee4\u8f93\u51fa\u7684\u89e3\u91ca<\/li>\n<li><a href=\"http:\/\/www.ibm.com\/developerworks\/aix\/library\/au-lsof.html\">http:\/\/www.ibm.com\/developerworks\/aix\/library\/au-lsof.html <\/a>\u00a0#\u5168\u9762<\/li>\n<li><a href=\"http:\/\/www.tecmint.com\/10-lsof-command-examples-in-linux\/\">http:\/\/www.tecmint.com\/10-lsof-command-examples-in-linux\/ <\/a>\u00a0#\u6837\u4f8b<\/li>\n<li><a href=\"https:\/\/danielmiessler.com\/study\/lsof\/\">https:\/\/danielmiessler.com\/study\/lsof\/<\/a><\/li>\n<li><a href=\"http:\/\/www.thegeekstuff.com\/2012\/08\/lsof-command-examples\/\">http:\/\/www.thegeekstuff.com\/2012\/08\/lsof-command-examples\/<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u641c\u7d22\u5173\u952e\u5b57\uff1a linux lsof search by filename \u53c2\u8003\u5185\u5bb9\uff1a \u5f88\u65e9\u4e4b\u524d\u6211\u5c31\u542c\u8bf4\u8fc7lso [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,12],"tags":[30,332],"class_list":["post-2156","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-tools","tag-linux","tag-lsof"],"views":4425,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2156"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2156\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}