Linux 使用 lsof 检查文件是否打开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17321930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:18:47  来源:igfitidea点击:

check if file is open with lsof

linuxbashfile

提问by rafa

I'm using linux mint 13 xfce and I have a file named wv.goldthat I'm trying to check in bash if it's open by any program (for instance, I opened it in sublime-textand gedit)

我正在使用 linux mint 13 xfce 并且我有一个名为的文件wv.gold,如果它被任何程序打开,我正在尝试检入 bash(例如,我在sublime-text和 中打开它gedit

In many forums people say that if I run lsof | grep filenameI should get 0if it's open or 256(1)if it's closed, but in fact I get nothing (empty string) if I run using grep "wv.gold", and get a little list if I do it using grep gold.

在许多论坛中,人们说如果我运行,lsof | grep filename我应该知道0它是打开的还是256(1)关闭的,但实际上,如果我使用 运行grep "wv.gold",我什么也得不到(空字符串),如果我使用grep gold.

The list is something like:

该列表类似于:

bash       2045  user   cwd   DIR   8,1     4096     658031 /home/user/path/to/dir
bash       2082  user   cwd   DIR   8,1     4096     658031 /home/user/path/to/dir
watch      4463  user   cwd   DIR   8,1     4096     658031 /home/user/path/to/dir
gedit     16679  user   cwd   DIR   8,1     4096     658031 /home/user/path/to/dir
lsof      20823  user   cwd   DIR   8,1     4096     658031 /home/user/path/to/dir
grep      20824  user   cwd   DIR   8,1     4096     658031 /home/user/path/to/dir
lsof      20825  user   cwd   DIR   8,1     4096     658031 /home/user/path/to/dir

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 geditprocess, not to sublime-textprocess.

因此,我得到了它所在目录的路径,但不是文件的路径(那里还有其他文件),无论哪种方式都只能gedit处理,而不是sublime-text处理。

Is there some easy way to see if a txt file is opened by any other program?

是否有一些简单的方法可以查看 txt 文件是否被任何其他程序打开?

EDIT: 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. 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.

编辑:事实证明(参见@mata 和@ctn 的评论)一些编辑器加载文件并立即关闭它们,并且在保存文件时他们只是重新打开文件。这样,我们只有在他们还在打开一个大文件时才能看到它(因为你有时间在打开时观察它),然后它立即消失。

采纳答案by ctn

The lines that appear in the output of lsofare open files. If your file is not there it means it is not open. Among the columns are PID (the process id of the program that has the file open) and the FD (the file descriptor associated with the open file). No particular value for these indicates open/closed. If it appears at all it means it's open.

输出中出现的行lsof是打开的文件。如果您的文件不存在,则表示它未打开。这些列中有 PID(打开文件的程序的进程 ID)和 FD(与打开的文件关联的文件描述符)。这些没有特定的值表示打开/关闭。如果它出现,则表示它已打开。

Check out http://linux.die.net/man/8/lsofand search for the text contains the first nine characters

查看http://linux.die.net/man/8/lsof并搜索文本contains the first nine characters