0

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.

Googlebot
  • 1,959
  • 3
  • 26
  • 41
  • i recommend to add example output of lsof as this may slightly different on other OS – alecxs Jan 25 '21 at 20:42
  • You want to list/count all the open files of all the processes of command X. Is that correct? – ctrl-alt-delor Jan 25 '21 at 20:51
  • @ctrl-alt-delor yes exactly. – Googlebot Jan 25 '21 at 21:14
  • Edit question to make clear. – ctrl-alt-delor Jan 25 '21 at 21:23
  • @ctrl-alt-delor how should I edit the question? The sentence of the question How can we list/count the files opened by a specific program/COMMAND (e.g., java)? is exactly the sentence of your clarification You want to list/count all the open files of all the processes of command X.! – Googlebot Jan 25 '21 at 21:53
  • 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). – ctrl-alt-delor Jan 25 '21 at 22:08
  • Comments are designed for US to ask YOU questions about your Question. You should [Edit] your question to add information. By updating your Question, and using the formatting buttons, you make all the information available to new readers. People shouldn't have to read a long series of comments to get the whole story. – waltinator Jan 26 '21 at 17:01
  • @ArtemS.Tashkinov the question you linked is the example I gave in the original question. It lists/counts per process, but I want per program. I want the count of all files opened by all processes of java command. – Googlebot Jan 26 '21 at 23:06
  • @waltinator I didn't explain anything in the comment. A sentence was suggested for the clarification, and I quoted that the exact same sentence is already in the question. Which part of the comments do you recommend to be included in the question? – Googlebot Jan 26 '21 at 23:08

1 Answers1

2

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.

GAD3R
  • 66,769
t4ng0
  • 36