According to my lsof's manpage, that's not what the -c option does:

This option selects the listing of files for processes executing the command that begins with the characters of c.

That filters by the name of the command, not the name of the open file.

But your lsof|grep example and the Perl one should have the same bahevious and should be comparable. I am not sure why they are so different. One important difference is that you are capturing output with backticks in one case and not in the other.

You should also be aware lsof's reporting of filenames may not always be reliable. Once you open a file, the kernel forgets what name was used to open it and only remembers which device and inode was opened. If the file has multiple names (hard links) then it is in fact impossible in general to know which one was opened. lsof gets around this by searching for the name in the kernel's directory name lookup cache where the kernel remembers this information in case it needs it again. But it is a cache and it might be flushed. Does your application permit finding exactly which files you are interested in and searching for those files in the lsof output by device & node number?


In reply to Re: system calls vs perl functions: Which should be faster in this example? by Celada
in thread system calls vs perl functions: Which should be faster in this example? by machinecraig

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.