Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of open files (descriptors) in OS X

I would like to get a list of open files in a process on os x (10.9.1). In Linux I was able to get this from /proc/PID/fd. However I'm not sure how to get the same on OS X. I found that the procfs is not present on the OS X (by default. possible implementations present, but I do not want to go that way).

So how do I get (natively) the list of open files in a process on OS X. One way is lsof. is there any other support available? please let me know where I can get more info on this.

Thanks.

like image 383
user3169543 Avatar asked Jan 07 '14 14:01

user3169543


People also ask

How can I see open file descriptors?

We can take advantage of the information exported under /proc and combine it with the ls command to list the open file descriptors of a process. In the /proc pseudo filesystem, we can find the open file descriptors under /proc/<pid>/fd/ where <pid> is the PID of a given process.

Where are open file descriptors stored?

On Linux, the set of file descriptors open in a process can be accessed under the path /proc/PID/fd/ , where PID is the process identifier.

How do I find the file descriptors of a process?

You can use /proc file system or the lsof command to find all the file descriptors used by a process.

How do you know how many file descriptors?

You can read /proc/sys/fs/file-nr to find the total number of allocated and free file system handles as well as the maximum allowed.


3 Answers

I had a hard time getting Activity Monitor to show open files for a process that was running as root (via sudo). The original question mentions lsof, and it does the trick exactly. If you know the process name or PID, it's super quick.

Find processes by name:

lsof -c processname

Find processes by PID:

lsof -p 123

(Prefix with sudo as needed, such as if you are not the owner of the process.)

like image 154
smitelli Avatar answered Oct 20 '22 06:10

smitelli


At least on OSX 10.10 (Yosemite, didn't check on Mavericks), you can get the list of open files by process via the default activity monitor application. Just double click on the relevant process on the list and select "Open Files and Ports" tab on the popup.

Tip: cmd+f shortcut on that pane allows for searching and highlighting on the content.

like image 42
Fuu Avatar answered Oct 20 '22 06:10

Fuu


This works for some things:

sudo fs_usage | grep dev

for /dev/ files or similar.

like image 13
eulerworks Avatar answered Oct 20 '22 07:10

eulerworks