or Connect
AppleInsider › Forums › Software › Mac OS X › A Mac OS X Tip
New Posts  All Forums:Forum Nav:

A Mac OS X Tip

post #1 of 9
Thread Starter 
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:

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.
--Johnny
Reply
--Johnny
Reply
post #2 of 9
No one has commented, but this is a great script. I've had this problem before, and this seems like a simple solution. So much so, that I wonder why Apple hasn't wrapped this into an error message that actually tells you something.

Thanks for the tip.
post #3 of 9
Quote:
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.
post #4 of 9
Thanks for the tip. That had always annoyed me.
post #5 of 9
Thread Starter 
I've done some experimenting, and it seems that most apps just re-write the file to the original destination even if you move the original to the Trash.

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.
--Johnny
Reply
--Johnny
Reply
post #6 of 9
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?
post #7 of 9
Also, while holding down the "alt/option" key go to finder --> empty trash. You'll notice that the name turns from "empty trash..." to "empty trash" and that the alt symbol appears in front of the key command as well...

It's force deleting for the masses
The bored one.
Reply
The bored one.
Reply
post #8 of 9
Quote:
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.
post #9 of 9
Thanks! I'll try your suggestions next time it happens. I don't think it is a Spotlight thing though; I first noticed this in Panther.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Mac OS X
AppleInsider › Forums › Software › Mac OS X › A Mac OS X Tip