tags:

views:

73

answers:

5

In Linux, how can I get a list of files currently open in program X?

(Specifically, I'm on Fedora, and the program is Zend Studio, but I assume there's a general answer.)

+7  A: 

Use e.g. lsof ("list open files") and filter the output, or use the -c programX option to restrict it to files opened by programX. This expression can be a wildcard.

Dirk Eddelbuettel
I've tried `lsof | grep somefile`, where `somefile` is a file I know is open in Zend Studio, but it finds nothing. Same thing if a file is open in gedit. Maybe these files aren't technically "open" except for when the program is actively reading or saving them, and the rest of the time it just has a copy in memory and a reference to where the file is stored?
Nathan Long
+1  A: 

Install the program lsof if you haven't already. Get the pid of the running program. Run lsof -p [pid].

Paul Tomblin
+4  A: 

You can find symbolic links to the actual files in /proc/PID/fd where PID is the process ID.

paxdiablo
Hmmm. I'm trying to determine the process ID - I did `ps -AF | grep zend` and got various listings (I have Zend Server running, etc), but nothing that looked like studio. I closed Zend Studio and tried again, and the list was the same. Any thoughts on how to determine the PID, other than "start killing them and see when it closes?"
Nathan Long
+2  A: 

ls -l /proc/<zend studio's pid>/fd will print a list of file descriptors. Those that map to files that were opened through open() will be symlinks to the file path used to open them.

Matt Joiner
A: 
lsof -p [PID]

Or, to get the PID in one go

lsof -p `pgrep [PROG_NAME]`

Or, even simpler

lsof -c [PROG_NAME]
Job