List Open Files

November 4th, 2008 by Jagadish Kavuturu in

Your rating: None Average: 4.6 (32 votes)

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.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

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 :-))

Hatta's picture

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?

SteveC's picture

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:

# all tcp connections
lsof -i tcp

# connections to mail server on 10.2.3.4
lsof -i 10.2.3.4:25

# What's connected to my PostgreSQL server via TCP/IP?
lsof -i :5432

# What is my browser connected to?
lsof -i :80
Anonymous's picture

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

Ivan V's picture

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.

Durand's picture

Thanks! That's a cool trick!

On November 6th, 2008 Durand (not verified) says:

Thanks! That's a cool trick!

Anonymous's picture

Thanks, a much needed tip.

On November 6th, 2008 Anonymous (not verified) says:

Thanks, a much needed tip.

vinit's picture

Nice tip

On November 6th, 2008 vinit (not verified) says:

Thanks for the gr8 tip.

Anonymous's picture

Short and to the point

On November 5th, 2008 Anonymous (not verified) says:

Short and to the point Howto. Nice.

Devin's picture

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]

Anthony Lawrence's picture

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

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Tech Tip Videos

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.





Read this issue