Command of the day - lsof

If you need to list all files that are currently open on your Linux or other Unix system, look no further than the lsof command.

On my system (Fedora Core 6), it's located in /usr/sbin/, meaning that you'll need to either run it as root, or specify the full path to the application:

$ /usr/sbin/lsof

It will show you a big long list of all the files that are currently open. On my system, the result from running the command was quite huge, so you might want to pipe the output through less, so you can page up and down through the output at your own pace (q to quit).

$ /usr/sbin/lsof | less

It's useful when you're trying to unmount a device like a USB drive or a CD and the OS won't let you. With lsof you can see exactly what's open, so you can close any offending files and try again (if you're reasonably proficient at the command line, you can grep through the clutter to find what you're looking for). It also comes in handy for other stuff too.

As always, you can read the man page for more advanced options on using the command.

Avatar for peter Peter Upfold - http://peter.upfold.org.uk/

Peter Upfold is a technology enthusiast from the UK. Peter’s interest in Linux stems back to 2003, when curiosity got the better of him and he began using SUSE 9.0. Now he runs Linux Mint 9 on the desktop, runs a CentOS-based web server from home for his personal website and dabbles in all sorts of technology things across the Windows, Mac and open source worlds.

Home » Articles »

Discussion: Command of the day - lsof

Home » Articles » Command of the day - lsof