What Apps or Processes Are Interacting With a File in macOS?
In this article
To see which apps or processes are interacting with a file in macOS, open Terminal and use the lsof command with the file's path, or use the built-in Activity Monitor and the fs_usage tool for real-time monitoring. The most direct command is lsof /path/to/file, which lists every process that currently has the file open for reading, writing, or execution.
Why You Might Need This Information
Knowing which processes are using a file is essential for several common tasks. You might be trying to delete or move a file but macOS reports it is “in use.” You could be troubleshooting why a file is locked, or you might suspect a background process is constantly reading or writing to a file, causing performance issues or unexpected disk activity. Developers often need to verify that their application has released a file handle after a test, and system administrators use this information to diagnose conflicts between services.
The lsof Command: Your Primary Tool
The lsof (list open files) utility is the standard Unix tool for this job. It has been part of macOS for decades and is installed by default. To use it, open Terminal (located in /Applications/Utilities/) and type:
lsof /path/to/your/fileReplace /path/to/your/file with the actual path. For example, if you have a document named report.pdf on your Desktop, the command would be:
lsof ~/Desktop/report.pdfThe output shows columns with the process name, PID (process ID), user, and the file descriptor type (e.g., txt for text, mem for memory-mapped, cwd for current working directory). The last column, “NAME,” shows the file path. If the file is not open, the command returns nothing.
Using lsof to Find Processes by Name or Port
You can also use lsof to find which processes are interacting with a file based on the process name or network port. For example, to see all files opened by a specific application, use:
lsof -c SafariThis lists every file Safari has open. To find which process is using a specific network port (useful for debugging server conflicts), use:
lsof -i :8080This shows the process listening on port 8080. Combining -c with a file path can narrow down results, but the basic lsof /path is usually sufficient.
Real-Time Monitoring with fs_usage and Activity Monitor
If you need to see file interactions as they happen, fs_usage is a powerful tool. It logs every file system call made by any process. Run it with sudo to get full output:
sudo fs_usageThe output updates continuously, showing the process name, the system call (e.g., open, read, write), and the file path. To filter for a specific file, pipe the output through grep:
sudo fs_usage | grep report.pdfThis is particularly useful when you suspect an app is repeatedly accessing a file but you don’t know which one. Activity Monitor, the graphical utility, also provides a “Disk” tab that shows per-process I/O statistics, but it does not show individual file paths. For that, you need fs_usage or lsof.
What to Do When a File Is “In Use” or Locked
When macOS refuses to move or delete a file, it’s often because a process has it open. First, try closing the obvious application—if it’s a document, quit the app that created it. If that fails, use lsof to identify the offending process, then quit it gracefully with kill PID (replace PID with the number from the output). If the process is unresponsive, you may need to use kill -9 PID, but this should be a last resort because it doesn’t allow the app to save data. For files locked by the system (like some in /System), you may need to restart your Mac or boot into Recovery Mode, but that is rare.
Common Pitfalls and Misconceptions
- Misconception: Activity Monitor shows which files are open. It shows per-process disk usage, but not individual file paths. You need Terminal.
- Pitfall: Running
lsofwithout sudo may miss some processes. Some system processes hide their file handles from non-root users. Usesudo lsof /pathto get complete results. - Pitfall: The file path must be exact. If you use a relative path,
lsofmight not match. Use the full path orcdto the directory first. - Misconception:
lsofonly works on files you own. It lists all processes, but you may need sudo to see other users’ processes.
Alternatives to lsof
If you prefer a graphical interface, third-party tools like lsof GUI or FSEventer provide a visual representation of file access. However, they are not built into macOS and require installation. The built-in lsof is always available and is the most reliable. For a simpler check, you can use the fuser command (though it’s not standard on macOS) or the openfiles command in some versions of macOS, but lsof is the most comprehensive.
Frequently Asked Questions
How do I find which process has locked a file?
Use lsof /path/to/file in Terminal. The output will list the process name and PID. If you get no output, the file is not currently open by any process.
Can I see all files a specific app is using?
Yes, run lsof -c AppName (e.g., lsof -c Safari). This lists all open files for that process. You can also use the PID: lsof -p PID.
Why does lsof show nothing even though I can’t delete the file?
If lsof returns nothing, the file might be locked by macOS itself (e.g., a system file) or have permissions that prevent deletion. Check the file’s flags with ls -lO to see if it has the “uchg” flag (user immutable), and remove it with chflags nouchg if appropriate.
Is there a way to monitor file access in real time without Terminal?
Activity Monitor shows disk activity per process, but not specific files. For real-time file paths, you must use Terminal commands like fs_usage or a third-party GUI tool.
What does the “txt” type mean in lsof output?
“txt” indicates that the file is a shared text file, usually a library or executable that the process has mapped into memory. It’s normal for many system libraries to appear as “txt” for multiple processes.