A Mac OS X Tip

Posted:
in macOS edited January 2014
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.

Comments

  • Reply 1 of 8
    kedakeda Posts: 722member
    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.
  • Reply 2 of 8
    gsxrboygsxrboy Posts: 565member
    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.
  • Reply 3 of 8
    Thanks for the tip. That had always annoyed me.
  • Reply 4 of 8
    lundylundy Posts: 4,466member
    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.
  • Reply 5 of 8
    jbljbl Posts: 555member
    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?
  • Reply 6 of 8
    ichiban_jayichiban_jay Posts: 660member
    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
  • Reply 7 of 8
    MarvinMarvin Posts: 15,326moderator
    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.
  • Reply 8 of 8
    jbljbl Posts: 555member
    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.
Sign In or Register to comment.