A Mac OS X Tip
Ever had Finder say "The operation cannot be completed because the item "foo.txt" is in use", when you try and empty the Trash?
Except you look at all your apps and NONE of them appear to have "foo.txt" open?
Just open Terminal,
type "lsof " (notice the trailing space)
open the Trash window by clicking on the Trash icon in the Dock.
Drag the "foo.txt" or whichever item is "stuck" into the Terminal window.
Hit return. lsof will tell you which process has the file open. You can then quit that app to release the file (even though the app itself claims that it doesn't have the file open).
Here is some sample output for "stuck open" file "1148.pdf" which I opened in Preview and then trashed the file while Preview had it open:
Dual-G5:~/Desktop lundy$ lsof /Users/lundy/.Trash/1148.pdf
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
Preview 7639 lundy txt VREG 14,3 136144 1162021 /Users/lundy/.Trash/1148.pdf
Dual-G5:~/Desktop lundy$
Under "COMMAND" above, lsof tells us that the file in question is opened by the process "Preview". Voila.
I think this could be made into a script that would take ALL items in the Trash and list which apps have them open. I'm working on it.
Except you look at all your apps and NONE of them appear to have "foo.txt" open?
Just open Terminal,
type "lsof " (notice the trailing space)
open the Trash window by clicking on the Trash icon in the Dock.
Drag the "foo.txt" or whichever item is "stuck" into the Terminal window.
Hit return. lsof will tell you which process has the file open. You can then quit that app to release the file (even though the app itself claims that it doesn't have the file open).
Here is some sample output for "stuck open" file "1148.pdf" which I opened in Preview and then trashed the file while Preview had it open:
Code:
Dual-G5:~/Desktop lundy$ lsof /Users/lundy/.Trash/1148.pdf
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
Preview 7639 lundy txt VREG 14,3 136144 1162021 /Users/lundy/.Trash/1148.pdf
Dual-G5:~/Desktop lundy$
Under "COMMAND" above, lsof tells us that the file in question is opened by the process "Preview". Voila.
I think this could be made into a script that would take ALL items in the Trash and list which apps have them open. I'm working on it.
Comments
Thanks for the tip.
Originally posted by lundy
You can then quit that app to release the file...
Normally just quitting the original app works for me.. it used to be mail all the time that did it but now its only preview. Thx for the scripty script.
For example, in PhotoShop and TextEdit, the Finder does not complain if you empty the Trash with an open document in the Trash. lsof also does not flag it in the Trash.
So the best thing is probably to just quit Preview - it seems to be the culprit in almost all of the cases. Even a mounted dmg, moved to the Trash, doesn't trigger lsof.
It's force deleting for the masses
Originally posted by JBL
I have never had this problem with emptying the trash. However, I have it all the time when I try to unmount disks. lsof helps sometimes but there have been cases when the Finder complains that a disk is in use when lsof doesn't list any file on that disk. What's up with that?
Yeah, I noticed that too. You can try using
hdiutil eject -force /dev/disk0s3
where instead of disk0s3, you use the ID of your disk image. You can find this out by typing df in the terminal.
I don't know if it's a Spotlight thing. If the drive is being indexed or something then maybe the Finder sees it as in use but lsof doesn't? It still shouldn't happen whatever it is.