20

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)

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.

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 gedit process, not to sublime-text process.

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

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.

3
  • 7
    try fuser from psmisc. Should be available as psmisc package for most distributions. Note that many text editors don't keep an open file handle to the files they open. They read the file, close it and write it back on save.
    – mata
    Commented Jun 26, 2013 at 13:57
  • Make sure you're running lsof as root user, otherwise it will be severely limited in what it shows
    – Magnus
    Commented Jun 26, 2013 at 14:32
  • Related: How to find out which process is using a file in linux
    – kvantour
    Commented Jun 5, 2019 at 7:18

1 Answer 1

18

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.

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

6
  • 1
    but still, when running lsof | grep "wv.gold" when it's open in gedit, I get no lines printed
    – rafa
    Commented Jun 26, 2013 at 14:18
  • 2
    As mata commented on your question, editors can open the file, read it fully into memory and close the file.
    – ctn
    Commented Jun 26, 2013 at 14:23
  • 2
    If you want to check out what processes are currently opened by gedit first get gedit's pid: pgrep gedit, and then the list of open files: ls -l /proc/<pid>/fd. You should see symbolic links of the form 4 -> /some/file.txt
    – ctn
    Commented Jun 26, 2013 at 14:28
  • I ran sudo ls -l /proc/<pid>/fd and got this numbers pointing to this files, but not the open file wv.gold 0 -> /dev/null 1 -> pipe:[7587] 10 -> anon_inode:[eventfd] 11 -> anon_inode:inotify 12 -> socket:[298116] 13 -> /home/user/.local/share/gvfs-metadata/home 14 -> /home/user/.local/share/gvfs-metadata/home-9e16308f.log 2 -> pipe:[7587] 3 -> socket:[301059] 4 -> anon_inode:[eventfd] 5 -> socket:[298112] 6 -> socket:[296715] 7 -> anon_inode:[eventfd] 8 -> anon_inode:[eventfd] 9 -> socket:[298115]
    – rafa
    Commented Jun 26, 2013 at 15:18
  • 1
    That means wv.gold is not kept open by gedit. It's fully read into memory and closed. gedit will only open it again to read changes that might have happened or to write the file back to disk. There isn't really any (generic) way to do what you want. The only possibility is if gedit keeps a swap file around for each open file.
    – ctn
    Commented Jun 26, 2013 at 15:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.