Given that you have a specific target file (hence its inode and the number of links to that inode), then why not just use the normal unix "find" utility:
use strict; my ($dir,$file) = @ARGV; my ($finode,$fnlinks) = (lstat($file))[1,3]; $/ = chr(0); my @hardlinks = `find $dir -inum $finode -print0`; chomp @hardlinks; # get rid of null-byte terminations printf "found %d of %d links for %s (inode %d) in %s:\n", scalar @hardlinks, $fnlinks, $file, $finode, $dir; print join("\n",@hardlinks),"\n";
I haven't tried benchmarking that but based on prior experience, if you happen to be searching over any really large directory trees (thousands of files), I know that this approach will be at least 5 or 6 times faster than any solution involving File::Find. (I have posted at least three benchmarks on PM to prove this.)

It also seems a lot simpler. Since you're looking specifically for hard links (files with identical inodes), the issue of portability to non-unix systems is irrelevant.

The unix "find" command is the right tool for this job (and perl just makes it easier to use "find", which is worthwhile).

(update: simplified the "printf" statement a little; also should clarify that the "5 to 6 times faster" is in terms of wall-clock time to finish a given run.)

(another update: after simplifying the printf, I put the args in the right order so that the output is correct.)


In reply to Re: aborting File::Find::find by graff
in thread aborting File::Find::find by marvell

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.