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
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- My 10 UNIX Command Line Mistakes
- Linux: 20 Iptables Examples For New SysAdmins

- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
Facebook it - Tweet it - Print it -


{ 13 comments… read them below or add one }
Here is a cool lsof trick that shows you what port is in use by which program:
lsof -iTCP:80
lsof -p `ps -C firefox -o pid=`
in a single line
For the single line lovers, here is another:
lsof -p `pgrep firefox-bin`
pgrep could be not automatically installed by your distro.
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”
Install lsof package.
netstat -vatupn
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.
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.
I’m afraid there are no way to get process which created file. :(
What a pity! Thks for your help man.
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
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
What is man difference between lsof & pmap?