About Linux FAQ

Browse More FAQs:

Linux / UNIX: List Open Files for Process

Posted by Vivek on Wednesday March 5, 08 @5:08 pm

Q. How do I list all open files for a Linux or UNIX process using command line options?

A. Both Linux and UNIX like operating systems comes with various utilities to find out open files associated with process.

Task: UNIX List Open Files For Process

First use ps command to get PID of process, enter:
$ ps -aef | grep {process-name}
$ ps -aef | grep httpd

Next pass this PID to pfiles command,
$ pfiles {PID}
$ pfile 3533

Task: Linux List Open Files For Process

First you need to find out PID of process. Simply use any one of the following command to obtain process id:
# ps aux | grep {program-name}
OR
$ ps -C {program-name} -o pid=
For example, find out PID of firefox web-browser, enter:
$ ps -C firefox -o pid=
Output:

 7857

To list opne files for firefox process, enter:
$ ls -l /proc/7857/fd
Sample output:

total 0
lr-x------ 1 vivek vivek 64 2008-03-05 22:31 0 -> /dev/null
l-wx------ 1 vivek vivek 64 2008-03-05 22:31 1 -> pipe:[18371]
lr-x------ 1 vivek vivek 64 2008-03-05 22:31 10 -> /usr/lib/firefox/firefox
l-wx------ 1 vivek vivek 64 2008-03-05 22:31 2 -> pipe:[18371]

lsof command

lsof command list open files under all Linux distributions or UNIX like operating system. Type the following command to list open file for process ID 351:
$ lsof -p 351

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Linux / UNIX FAQ:

Discussion on This FAQ

  1. Robert de Bock Says:

    Here is a cool lsof trick that shows you what port is in use by which program:

    lsof -iTCP:80

  2. Eduardo GarcĂ­a Says:

    lsof -p `ps -C firefox -o pid=`
    in a single line

  3. Marco Says:

    For the single line lovers, here is another:

    lsof -p `pgrep firefox-bin`

    pgrep could be not automatically installed by your distro.

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Please do not use the comment form to ask for help / question. Ask your question on the excellent Linux tech support forum. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , , ~ Last updated on: March 5, 2008

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.