jcb has asked for the wisdom of the Perl Monks concerning the following question: (files)

On POSIX systems, and possibly others, a file can have multiple names (known as hardlinks) or can itself contain the name of another file in such a way that the system substitutes that other file for most operations on a file (these are symlinks). For various platforms, are similar features available, and, if so, how best to determine if two seemingly different names are in fact the same file?

This question was also asked in Seekers of Perl Wisdom as Portable way to determine if two names refer to the same file?. The discussion there may also be of interest.

Originally posted as a Categorized Question.

  • Comment on How do I portably determine if two filenames refer to the same file?

Replies are listed 'Best First'.
Re: How do I portably determine if two filenames refer to the same file?
by jcb (Parson) on Aug 16, 2019 at 03:40 UTC

    For POSIX systems, the first two values in the list returned by Perl's stat builtin provide a solution. These are the "device" and "inode" numbers for the file and, taken as a pair, uniquely identify a file.

    # return true if both names are for the same file sub check_same_file_under_posix { my ($file_a, $file_b) = @_; my ($dev_a, $ino_a, @other_a) = stat($file_a); my ($dev_b, $ino_b, @other_b) = stat($file_b); if (($dev_a == $dev_b) && ($ino_a == $ino_b)) { return 1 } else { return 0 } }