We can list only files opened by a specific PID as

lsof -p 1000
lsof -p 1000 | wc -l

How can we list/count the files opened by a specific program/COMMAND (e.g., java)?

And so much better, if we can group the number of open files for each program. I want to inspect which programs have high numbers of opened files.

I want something like

lsof -c "java" # -c is an imaginary argument similar to -p for process

I use Ubuntu 20.04.

  • i recommend to add example output of lsof as this may slightly different on other OS
    – alecxs
    Jan 25, 2021 at 20:42
  • You want to list/count all the open files of all the processes of command X. Is that correct? Jan 25, 2021 at 20:51
  • @ctrl-alt-delor yes exactly.
    – Googlebot
    Jan 25, 2021 at 21:14
  • Edit question to make clear. Jan 25, 2021 at 21:23
  • 1
    Yes. Exactly. However my clarification is down here in the comments. People don't want to read an unclear question, then read all the comments in the hope that it will eventually make sense (OK this is an exaggeration, your question is not that bad, but I exaggerate to make a point). Jan 25, 2021 at 22:08

1 Answer 1


I don't think there is an argument for such a thing implemented on lsof and I don't know what flags are available on your lsof binary.
I think you could achieve what you want with something like this, maybe replacing the head with a 'grep java':

lsof | awk '{print $1}' | sort | uniq -c | sort -rn | head

lsof: Basically I'm listing all the opened files
awk '{print $1}': printing only the first column which is the process name
sort: you need to sort before applying uniq otherwise it will split the count, so java may appear several times depending on the order lsof prints.
uniq -c: group by process name and count lines
The last 2 are for readability.

The problem with this is that all the java instances will be combined, I suppose you could apply the same logic for PIDs and then filter your java instances and child processes by PID.

Hope it helps.

  • works perfectly!
    – Googlebot
    Jan 29, 2021 at 8:15

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