Files being used by a unix process

lamcro picture lamcro · Nov 19, 2008 · Viewed 46.9k times · Source

The fuser command lets me know which processes are using a file or directory.

I'm looking for command that does the opposite: lets me know which files are being used by a process.


Update

Forgot to mention that it's for a Solaris system.

Answer

Johannes Schaub - litb picture Johannes Schaub - litb · Nov 19, 2008
lsof -p <pid>

From here

lsof stands for “LiSt Open Files”. This shell command seems deceptively simple: It lists information about files opened by processes on a UNIX box.

Despite its (apparent) modest mission statement, lsof is actually one of the most powerful and useful UNIX commands. Its raw power comes from one of UNIX’s design principle often described as ”in UNIX everything is a file”. What this means is that the lsof concept of an open file not only covers regular files but also the following:

  • Directories
  • Streams or network files (for example, Internet or UNIX domain sockets and NFS files)
  • Native libraries (for example, .soor .dylibdynamic libraries linked to a process)
  • Block and character special files (for example, disk volume, external hard drive, console, or mouse)
  • Pipes

Wait, I Cannot Find lsof on My System!

lsof is such a popular tool that it has been ported to pretty much all UNIX dialects (Linux, Mac OS X, BSD, Solaris, and so on). If it is unavailable on your box, use your usual package management system to install it. You can find lsof packages for Solaris on Sun Freeware.