Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use lsof to find 'file in use' culprits UNIX

Ever try to empty your trash, only to find that your request is refused because apparently some program is still using that file, so you get this nice message:

The operation cannot be completed because the item [whatever] is in use.

Sometimes this is a handy sanity check. Other times it's a downright nuisance, because you're just SURE that no program should legitimately be using that file. It happens to me frequently enough that I figure someone else could benefit from a little UNIX lore. The command-line UNIX program lsof rides to the rescue. This program lists all open files and file-like things. This is very handy, as nearly everything looks like a file in UNIX (and Mac OS X).

$ lsof | grep [whatever]

Where [whatever] is replaced with the filename you're looking for. With this, you can see which program is desperately holding onto your about-to-be-trashed file. Once you exit that program, your trash will empty.

    •    
  • Currently 3.86 / 5
  You rated: 4 / 5 (7 votes cast)
 
[62,011 views]  

Use lsof to find 'file in use' culprits | 12 comments | Create New Account
Click here to return to the 'Use lsof to find 'file in use' culprits' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use lsof to find 'file in use' culprits
Authored by: stevec on Jan 29, '04 04:10:49PM

Just in case LSOF and grep doesn't work the first time for you like it didn't for me, LSOF is case sensitive!

lsof | grep mail

got me nothing

lsof | grep Mail

now THAT works!



[ Reply to This | # ]
Use lsof to find 'file in use' culprits
Authored by: zs on Jan 29, '04 05:32:31PM

It's actually grep that is case sensitive. Use 'grep -i' to ignore case.

---
zs



[ Reply to This | # ]
Use lsof to find 'file in use' culprits
Authored by: mervTormel on Jan 29, '04 07:23:25PM

run lsof as root to see all user's open files



[ Reply to This | # ]
Use lsof to find 'file in use' culprits
Authored by: jiclark on Jan 29, '04 11:01:46PM

Will this work for those stubborn CDs and mounted .dmg files that say they can't be ejected because they're "...in use"?

I'm guessing yes, and hoping I'm right!



[ Reply to This | # ]
Grep not needed
Authored by: extra88 on Jan 29, '04 11:36:07PM

If you know which file in Trash is in use (which apparently you do since you're grepping for its name), open Terminal, enter "lsof " (mind the space) then drag the file's icon from Trash's Finder window into the Terminal window. That'll put the full path of the file on the command line so just hit Return and lsof will spit back the program information.

The grep method is useful for the other person who can't unmount volumes because a file on them is in use.



[ Reply to This | # ]
Use lsof to find 'file in use' culprits
Authored by: henry on Jan 30, '04 07:12:49AM

I don't seem to be able to get this to work (either that, or I'm just failing to notice something obvious). To test this I mounted an image file in Finder, then deleted the .dmg file (without ejecting the mounted image). When I try to empty the trash I get the 'File in use' message, but when I enter the command...

lsof /Users/henry/.Trash/foo.dmg

...I don't get any output. I've also tried...

lsof | grep foo.dmg

...but that didn't return anything either. This seems like it could be a very useful command (especially if it can be used via OnMyCommand), if only I could get it to work.

Could it be that a mounted .dmg file isn't registered as the kind of file that lsof looks for?



[ Reply to This | # ]
Use lsof to find 'file in use' culprits
Authored by: semiotek on Feb 05, '04 11:27:09AM

Doesn't work for me either - Nick



[ Reply to This | # ]
Use lsof to find 'file in use' culprits
Authored by: jamiller1 on Apr 21, '04 12:39:35PM

Help. We are getting the same error message about the file is in use but it is when we try to delete a file from a server or when we try to replace a file on the server. We just started using OSX within the past few weeks, I know better late than never, and we are working with a Netware 6 server.



[ Reply to This | # ]
Use lsof to find 'file in use' culprits
Authored by: -dl- on Nov 11, '04 10:33:39AM

If the culprit turns out to be the Finder you can release the file by doing a "Force Quit" on the Finder and choosing relaunch.

hth -dl-



[ Reply to This | # ]
Use lsof to find 'file in use' culprits
Authored by: magikeye on Jun 10, '06 09:53:47AM

there's always good old rm -f. that never complains. 'rm -f <pathtofile>' will take care of anything the trash bin can't.



[ Reply to This | # ]
Use lsof to find 'file in use' culprits
Authored by: vocaro on Jan 14, '07 10:38:18AM
Note that lsof might not find certain processes if they're owned by root. To solve that problem, simply run lsof as root, like this: sudo lsof | grep ...

[ Reply to This | # ]
Use lsof to find 'file in use' culprits
Authored by: lowbatteries on Jun 18, '08 03:01:32PM
I was thinking how great it was if Apple included some way to view the details of these errors, even just a simple list.

Then by accident I stumbled on "What's Keeping Me?", a nice GUI to find those troublesome files/processes that are open.

http://www.hamsoftengineering.com/products/wkm/wkm.html

[ Reply to This | # ]