in reply to Test to see if directories are the same

Here's a simple way to test if two paths point to the same file or directory, using stat:
sub matching_paths { my($path1, $path2) = @_; my(@stat1) = stat $path1; my(@stat2) = stat $path2; return $stat1[0] == $stat2[0] && $stat1[1] == $stat2[1]; }
(stat)[0, 1] are the device number and the inode number, which together uniquely identify a file or directory.

I don't know how portable this solution is, however.

Replies are listed 'Best First'.
Re: Re: Test to see if directories are the same
by merlyn (Sage) on Jan 17, 2001 at 23:54 UTC
    Or, a bit more obscure but very cool:
    sub same_file_p { my @stat1 = stat shift; my @stat2 = stat shift; return "@stat1[0,1]" eq "@stat2[0,1]"; }
    And even weirder:
    sub all_same_file { # true if all arguments represent the same file my %counts; $counts{join " ", (stat)[0,1]}++ for @_; 1 == keys %counts; }

    -- Randal L. Schwartz, Perl hacker

      Inode numbers are not reported on Windows so the above is OS-specific, as is this version:
      sub all_same_file { my @desc = map {my @s = stat; "@s[0,1]"} @_; @_ == grep {$desc[0] eq $_} @desc; }

      And slightly weirder:

      sub all_same_file { join(" ",map{(stat)[0,1]}@_) =~ /^(\d+ \d+) (\1 ?){$#_}$/; }