Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I knows I can use the command

lsof -p xxxx | wc -l

to know the count of opened files op a process, it works, but however, it is just too inefficient. I have some server process which have too many socket files, the wc -l method never return the result. So, what is the efficient way to know how many files opened on a process?


share|improve this question

There is a proc interface for open files: /proc/PID/fd/. It is a directory of symlinks. Any open sockets will appear to be linked to a file named "socket:[INODE NUMBER]"

share|improve this answer

Use ps -C <progname> -o pid= | wc -l where progname is the name of the process in a ps-listing.

share|improve this answer
That gives a count of processes not the count of open files. – Dennis Williamson Jun 10 '10 at 19:07
Sorry, my explanation wasn't specific enough. As described by Andrew E. Falcon, these are file descriptor PIDs. You can see which file is behind a PID by running it that way: for pid in $(ps -C <progname> -o pid=); do ls -l /proc/$pid/fd; done – weeheavy Jun 14 '10 at 8:34

Some context (use-case scenarios) may provide better direction for answers.

Documentation of lsof describes a few methods that it calls that may block for undesirable amounts of time. I would suggest glancing over the BLOCKS AND TIMEOUTS section of the lsof man page.

You might try the following and see if it provides desirable results:

lsof -bwp $pid | wc -l
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.