Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if file is open with lsof

Tags:

file

linux

bash

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.

like image 830
rafa Avatar asked Jun 26 '13 13:06

rafa


People also ask

How do you check if a file is open by a process?

You should use the fstat command, you can run it as user : The fstat utility identifies open files. A file is considered open by a process if it was explicitly opened, is the working directory, root directory, jail root directory, active executable text, or kernel trace file for that process.

How can I tell if a file is open in Linux?

The command lsof -t filename shows the IDs of all processes that have the particular file opened. lsof -t filename | wc -w gives you the number of processes currently accessing the file.

How do you check what process is using a file Linux?

Lsof is used on a file system to identify who is using any files on that file system. You can run lsof command on Linux filesystem and the output identifies the owner and process information for processes using the file as shown in the following output.

What is use of lsof command?

The lsof (list open files) command returns the user processes that are actively using a file system. It is sometimes helpful in determining why a file system remains in use and cannot be unmounted.


1 Answers

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

like image 181
ctn Avatar answered Oct 07 '22 07:10

ctn