List Open Files
November 4th, 2008 by Jagadish Kavuturu in
If you try to unmount a partition and get a message like this:
# umount /media/usbdisk/ umount: /media/usbdisk: device is busy
use the lsof command to find out what programs are using what files:
# lsof /media/usbdisk/ COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME bash 6925 root cwd DIR 8,17 4096 1 /media/usbdisk/ xmms 6979 root cwd DIR 8,17 4096 1 /media/usbdisk/
This shows that the programs bash and xmms are using the device. For an even clearer picture, use the device name rather than the mountpoint:
# lsof /dev/sdb1 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME bash 6925 root cwd DIR 8,17 4096 1 /media/usbdisk xmms 6979 root cwd DIR 8,17 4096 1 /media/usbdisk xmms 6979 root 8r REG 8,17 2713101 377 /media/usbdisk/a.mp3
You either can wait until those processes exit or terminate them manually.
__________________________
Jagadish Kavuturu
Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer
Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.
Subscribe now!
The Latest
Newsletter
Tech Tip Videos
- Jul-01-09
- Jun-29-09
Recently Popular
From the Magazine
July 2009, #183
News Flash: Linux Kernel 3.0 to include an on-the-go Expresso machine interface! Ok, maybe not, but Linux is definitely going mobile, from phones to e-readers. Find out more inside about Android, the Kindle 2, the Western Digital MyBook II, The Bug, and Indamixx (a portable recording studio). And if you've gone mobile and you been wanting more Emacs in your life then check out Conkeror.
To compliment the mobile we've got the stationary: parsing command line options with getopt, checking your Ruby code with metric_fu, and building a secure Squid proxy. How is this stationary you ask? What can we say? It's not. We just wanted to see if anybody actually read this part of the page :) .
All this and more, and all you have to do is get your hot sweaty hands on the latest copy of Linux Journal.
Delicious
Digg
StumbleUpon
Reddit
Facebook








That's crazy! but what the hey, sometimes we're lazy :)
On November 19th, 2008 maskedfrog says:
Thanx for the tip Anthony,
THAT will come in handy. The times I've waited for a cd to umount
and waited...and waited...and gave up and rebooted :-(
More apt to use in the examples shown though, waiting for a process to finish and don't want to forget to umount so just do it the "lazy" way.
Uptimes should shoot thru the roof now :-))
Slow
On November 15th, 2008 Hatta (not verified) says:
In my experience 'lsof' is extremely slow. So slow that I can log off and log back on before I'd expect lsof to finish. Are there any tricks to speed it up to the point that it's usable?
A very powerful command
On November 10th, 2008 SteveC (not verified) says:
The lsof command is far more powerful than the article implies. It's worth spending some time reading the documentation. My most common use is to locate connections with the -i option:
Killing with fuser
On November 10th, 2008 Anonymous (not verified) says:
If
lsof +D [device]
shows nothing
you can use
fuser -m [device]
to show processes that say that are using the device.
and you can use
fuser -km [device]
to directly kill them all
Doesn't always work
On November 7th, 2008 Ivan V (not verified) says:
Unfortunately, many times, neither lsof nor fuser have worked for me (the device says busy but nothing comes up)...
The only solution in those cases is for me to logoff or try to find a possible culprit.
Thanks! That's a cool trick!
On November 6th, 2008 Durand (not verified) says:
Thanks! That's a cool trick!
Thanks, a much needed tip.
On November 6th, 2008 Anonymous (not verified) says:
Thanks, a much needed tip.
Nice tip
On November 6th, 2008 vinit (not verified) says:
Thanks for the gr8 tip.
Short and to the point
On November 5th, 2008 Anonymous (not verified) says:
Short and to the point Howto. Nice.
Don't forget fuser!
On November 4th, 2008 Devin (not verified) says:
You can also use fuser if you know which directory or file is open and you want to find the offending process.
fuser [file]
You can also use the "lazy
On November 4th, 2008 Anthony Lawrence (not verified) says:
You can also use the "lazy umount" - see http://aplawrence.com/Linux/lazy-unmount.html for examples.
Post new comment