Identify the files opened a particular process on linux

aladine picture aladine · Apr 21, 2010 · Viewed 20.9k times · Source

I need a script to identify the files opened a particular process on linux

To identify fd :

>cd /proc/<PID>/fd; ls |wc –l  

I expect to see a list of numbers which is the list of files descriptors' number using in the process. Please show me how to see all the files using in that process. Thanks.

Answer

P Shved picture P Shved · Apr 21, 2010

The command you probably want to use is lsof. This is a better idea than digging in /proc, since the command is a more clear and a more stable way to get system information.

lsof -p pid

However, if you're interested in /proc stuff, you may notice that files /proc/<pid>/fd/x is a symlink to the file it's associated with. You can read the symlink value with readlink command. For example, this shows the terminal stdin is bound to:

$ readlink /proc/self/fd/0
/dev/pts/43

or, to get all files for some process,

ls /proc/<pid>/fd/* | xargs -L 1 readlink