http://www.perlmonks.org?node_id=350764

lsof is a tool to LiSt Open Files. It's a handy tool to have when you need to find out why you can't dismount a device, or see if a process is about to exceed its maximum number of allowed file handles. (Here's the project homepage).

But the output is verbose. If you just want the number of open files, and the process name and pid, the following one-liner performs a nice summary.

lsof | perl -lane '$x{"$F[0]:$F[1]"}++; END { print "$x{$_}\t$_" for sort {$x{$a}<=>$x{$b}} keys %x}'