Linux / UNIX: List Open Files for Process
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:
- How do I find out what ports are listening/open on my Linux/FreeBSD server?
- List the contents of a tar or tar.gz file
- Howto open .daa files (Direct-Access-Archive) under Linux / UNIX
- What files are in a RPM package?
- Connect to SQL Server from command prompt - list tables and database
Discussion on This FAQ
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!
Tags: command line options, grep program, Linux, linux distributions, list open files, ls command, lsof, lsof command, operating system, process id, ps command, UNIX ~ Last updated on: March 5, 2008



March 5th, 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
March 5th, 2008 at 6:50 pm
lsof -p `ps -C firefox -o pid=`
in a single line
April 20th, 2008 (4 weeks ago) 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.