I am using lsof to detect which files are opened by which process for a given directory. Example:
% lsof +D /Users/jack/Downloads
Spotify 1431 jack 75r DIR 1,6 128 37333 /Users/jack/Downloads/file1.png
Dock 1439 jack 13r DIR 1,6 128 37333 /Users/jack/Downloads/foo.psd
zsh 6644 jack cwd DIR 1,6 128 37333 /Users/jack/Downloads/foo.bmp
The man page of lsof(8)
states:
lsof may process this option slowly and require a large amount of dynamic memory to do it. This is because it must descend the entire directory tree, rooted at D, calling stat(2) for each file and directory, building a list of all the files it finds, and searching that list for a match with every open file. When directory D is large, these steps can take a long time, so use this option prudently.
Coincidentally I am already traversing the directory and call os.stat inside Python right before I call lsof
, means stat(2)
is technically called twice.
Which information does does the stat object hold that I could process myself to imitate the functionality of lsof
? Any help is highly appreciated!