Linux / UNIX: List Open Files for Process

by on March 5, 2008 · 20 comments· LAST UPDATED March 5, 2008

in , ,

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

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 20 comments… read them below or add one }

1 Robert de Bock March 5, 2008 at 5:22 pm

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

lsof -iTCP:80

Reply

2 Eduardo García March 5, 2008 at 6:50 pm

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

Reply

3 Marco April 20, 2008 at 2:57 pm

For the single line lovers, here is another:

lsof -p `pgrep firefox-bin`

pgrep could be not automatically installed by your distro.

Reply

4 abhi July 14, 2009 at 5:07 am

Hi,,,, lsof is not giving the results in my Linux server,
whenever i give the command ” lsof -u OSuser|wc -l ” its gives error saying “command not found”

Reply

5 nixCraft July 14, 2009 at 6:19 am

Install lsof package.

Reply

6 kolmisoft September 13, 2009 at 11:00 am

netstat -vatupn

Reply

7 eldar52 November 23, 2009 at 7:49 am

How can i see all files that opened by a specific pid and all it’s threads.
My C++ program opens 200 threads per process but i do not see all file descriptors that were opened by each thread.

Reply

8 javier December 4, 2009 at 11:16 am

Hi there,

Is it possible to do it in the opposite direction. How to know which process created a particular file?

I’ve just discovered your site, thanks a lot for sharing so much good stuff.

Javier.

Reply

9 kandrewo December 18, 2009 at 3:25 pm

I’m afraid there are no way to get process which created file. :(

Reply

10 Javier December 21, 2009 at 5:31 pm

What a pity! Thks for your help man.

Reply

11 shiprat April 7, 2010 at 4:42 am

javier:
>>> Is it possible to do it in the opposite direction. How to know which process created a particular file?

if the file is open try
fuser

Reply

12 Elijah Saounkine June 9, 2010 at 12:12 pm

fuser -m (or . in case file is not accessible)
will return all processes that have locked this file (or directory in case file is not accessible)
then proceed as suggested with lsof -p | grep

Reply

13 Nafasat April 15, 2011 at 3:02 pm

What is man difference between lsof & pmap?

Reply

14 omfg April 2, 2012 at 8:29 am

Oneliner for Linux ; )
lsof -p $(ps -ef | grep [PROGRAM_NAME] | grep -v grep | awk ‘{print $2}’)

Reply

15 ica May 4, 2012 at 4:48 pm

How could you get the files opened by a process using kernel modules?
I mean, what would be the corespondent for ps aux | grep ?

10x

Reply

16 Kiswono July 11, 2012 at 2:53 am

simpler way..

lsof -c firefox

Reply

17 Jane August 23, 2012 at 6:45 pm

it doesn’t works when i try to do ‘lsof -p ‘ and got message as below
/opt/csw/bin/lsof -p 22982
lsof: can’t stat(/devices): No such file or directory

Reply

18 spb December 6, 2012 at 7:35 pm

I am using weblogic osb in red hat linux system,and the command pfiles is not found.
My aim is to find File Descriptor’s total capacity

command which i am running is:
procfiles | grep rlimit

Please help.

Reply

19 paulvc April 23, 2013 at 1:06 am

My server doesn’t have lsof installed.

This works for me though:

ls -l /proc/*/fd | grep

Reply

20 winky April 29, 2013 at 5:29 am

Yeah… lsof comes from something that used to be called ‘ofiles’ I think…. Hit nothing searching for ‘ofiles’ so I’m including this comment ! ;)

Reply

Leave a Comment

Tagged as: , , , , , , , , , , ,

Previous Faq:

Next Faq: