检查文件是否使用lsof打开 [英] check if file is open with lsof
问题描述
我正在使用linux mint 13 xfce,我有一个名为wv.gold
的文件,我试图检查bash是否被任何程序打开(例如,我在sublime-text
和gedit
中打开了它) )
I'm using linux mint 13 xfce and I have a file named wv.gold
that I'm trying to check in bash if it's open by any program (for instance, I opened it in sublime-text
and gedit
)
在许多论坛中,人们说如果我运行lsof | grep filename
,我应该得到0
(如果它是打开的),或者得到256(1)
如果它是关闭的,但是实际上,如果我使用grep "wv.gold"
来运行,我什么也得不到(空字符串),并获得一些清单,如果我使用grep gold
来完成的话.
In many forums people say that if I run lsof | grep filename
I should get 0
if 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
.
列表类似于:
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
因此,我得到的是该目录的路径,但没有该文件的路径(那里还有其他文件),并且仅以一种方式访问gedit
进程,而不访问sublime-text
进程.
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.
是否有一些简单的方法来查看txt文件是否被其他程序打开了?
Is there some easy way to see if a txt file is opened by any other program?
编辑:事实证明(参见@mata和@ctn的评论),一些编辑器加载文件并立即将其关闭,并且他们只是在保存文件时重新打开了文件.这样,我们只有在他们仍在打开大文件时才能看到它(因为您有时间在打开文件时观察它),并且在此之后立即消失.
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.
推荐答案
lsof
输出中出现的行是打开的文件.如果您的文件不存在,则表示它没有打开.列中有PID(打开文件的程序的进程ID)和FD(与打开的文件关联的文件描述符).这些没有特别的值表示打开/关闭.如果它完全出现,则表明它已打开.
The lines that appear in the output of lsof
are 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.
检出 http://linux.die.net/man/8/lsof 并搜索文字contains the first nine characters
这篇关于检查文件是否使用lsof打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!