See what process is using a file in Mac OS X

MacosUnixTerminalAuditLsof

Macos Problem Overview


I would like to be able to track a file and see which process is touching that file. Is that possible? I know that I can see the list of open processes in activity monitor but I think it's happening to quickly for me to see it. The reason for this is I'm using a framework and I think the system version of the framework is being used instead of the debug version and I'd like to see which process is touching it.

Macos Solutions


Solution 1 - Macos

That's simple: sudo fs_usage | grep [path_to_file]

Solution 2 - Macos

lsof will list open files, but it can be a bit awkward for momentary touches (eg, if the file isn't open when lsof runs, it doesn't show).

I think your best bet would be fernLightning's fseventer.app. It's "nagware", and allows you to watch (graphically) the fsevents API in real-time.

Solution 3 - Macos

But I spent 2 minutes Googling and found your answer here.

> $ lsof | grep [whatever] > > Where [whatever] is replaced with the filename you're looking for. > With this, you can see which program is desperately holding onto your > about-to-be-trashed file. Once you exit that program, your trash will > empty.

Solution 4 - Macos

The faster way is:

$ lsof [path_to_file]

This solution doesn't require the root password and gives you back the following, clear, result:

COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF     NODE NAME
Finder     497  JR7   21r   REG    1,2   246223 33241712 image.jpg
QuickLook 1007  JR7  txt    REG    1,2   246223 33241712 image.jpg

Solution 5 - Macos

Another option is Sloth. It's a free, open source GUI for LSOF that others have mentioned.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJPCView Question on Stackoverflow
Solution 1 - Macosspaceboy.czView Answer on Stackoverflow
Solution 2 - MacosShaunView Answer on Stackoverflow
Solution 3 - MacosMichael DautermannView Answer on Stackoverflow
Solution 4 - MacosbontoJRView Answer on Stackoverflow
Solution 5 - MacoscavalcadeView Answer on Stackoverflow