How to Find Which Mac App Is Holding a File Open
macOS Sequoia and Tahoe; older versions noted where they differ · Last checked · Suggest an edit
https://instructions.wiki/mac/find-which-mac-app-is-holding-a-file-open
The Mac refuses to move, delete, or eject something because it is "in use," and the apps you would expect are already closed. The file is not corrupt; a process still has it open, and macOS will not release it until that process does. Finding that process and quitting it clears the lock.
Search for the file with lsof
Open Terminal from Applications, Utilities and run:
lsof | grep -i "part of the file name"
Use a distinctive piece of the file name. Each matching line lists the process name in the first column and its process ID (PID) in the second. The command can take several seconds to finish.
Quit the process in Activity Monitor
Open Activity Monitor, type the process name in the search field, select it, click the stop button in the toolbar, and choose Quit. If it does not respond, repeat and choose Force Quit. Then retry the move, delete, or eject.
List everything on a drive that will not eject
Run:
sudo lsof /Volumes/DriveName
Replace DriveName with the volume name shown in Finder, in quotes if it contains spaces. Adding sudo includes system processes. The output usually points at Spotlight indexing (mds or mds_stores), a Finder preview, or a Terminal window whose working directory is on that drive.
Move Terminal off the drive
If a Terminal line appears in the list, that window is sitting inside a folder on the drive. Run this in that window, then eject again:
cd ~
Close any Finder windows showing the drive as well.
Kill by PID if it will not quit
For a process that ignores Activity Monitor, run:
kill PID
with the number from the lsof output. Leave system processes such as mds alone; indexing finishes on its own within a few minutes, and the eject works after that.
If nothing shows up but the file still says in use. Quick Look and the Finder Preview pane count as opening the file. Close the Quick Look window and click on a different file. Time Machine or antivirus software scanning the file also makes it busy for a moment; wait a minute and try again.
If several things are stuck at once. Logging out and back in closes every process you own, and a restart closes the rest.