I am running few java processes on a machine and I wanted an exact count of number of open files and what kind those files.

First of all, running sudo lsof takes a long time.

[user@host]:~$ time sudo lsof > lsof.txt

real    1m5.920s
user    0m27.152s
sys 0m38.100s

Finding out the number of files opened by java processes:

[user@host]:~$ grep -e "^java" lsof.txt -c

which is unusually high.

If I try to arrive at the same number by filtering lsof's output using -c flag, I get a totally different answer and lsof takes very little time compared to the first command

[user@host]:~$ time sudo lsof -c java | wc -l

real    0m0.139s
user    0m0.052s
sys 0m0.068s

closed as unclear what you're asking by Timothy Martin, Stephen Rauch, mdpc, Kusalananda, user34720 Jun 27 '17 at 10:41

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


The speed inconsistency comes from the fact that sockets are also files, and other network related stuff like doing a stat() on an NFS. You can run lsof -b which will skip those operations.

As for the differencies in line numbers, well, you have to examine the result yourself. Sometimes it helps if you sort the result then compare it like so:

    sort lsof.txt > lsof-full-sorted.txt
    diff lsof-full-sorted.txt lsof-filtered-sorted.txt

(In my example lsof-filtered-sorted.txt refers to the output of lsof -c java|sort)

Also, midnight commander has a nice file compare functionality based on diff, which is easier to read than the diff output.

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