Too many open files

I am doing something that requires manipulating a lot of files, and I fell in the classical too many open files error trap.

A way of finding out which files are being used by a process is to type

ps -ax

in a terminal, then identify the guilty process and its PID. Let's imagine its PID is 9090. Now to list every one of its open files you just run this (again, in the terminal):

lsof -p 9090

Or you can get a raw estimate by piping that through wc and getting the number of lines in the return value of lsof:

lsof -p 9090 | wc -l

That will return a number, like for example "33".

It's interesting to know that the output from lsof does not only show files in the windows way of referring to a file as a folder or data written in the disk, but returns files in the UNIX way, i.e., everything is a file, including pipes, sockets, files-files, etc.

I have changed my code meanwhile to be a bit more austere in regards to the number of open files, but this is an interesting tool nevertheless. If you run it for example with Firefox, you can even see which font files Firefox is using:

firefox 3847 sole  mem    REG                8,1   224692   272248 /usr/share/fonts/truetype/msttcorefonts/Arial_Bold_Italic.ttf
firefox 3847 sole  mem    REG                8,1   622020     6209 /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf

Or which plug-ins has it loaded:

firefox 3847 sole  mem    REG                8,1   101536    13774 /usr/lib/mozilla/plugins/
firefox 3847 sole  mem    REG                8,1   117960      741 /var/lib/flashplugin-installer/

etc etc :)