How to tell if two paths point to the same file?

I’ve tried using File.expand_path to normalize path names, but this
doesn’t always seem to give identical results for the same file, if
one of the involved paths goes through a symbolic link. Is there
another way to do this?

Thanks,
Ken

Hi,

At Sat, 25 Oct 2008 02:25:42 +0900,
Kenneth McDonald wrote in [ruby-talk:318509]:

I’ve tried using File.expand_path to normalize path names, but this
doesn’t always seem to give identical results for the same file, if
one of the involved paths goes through a symbolic link. Is there
another way to do this?

File.identical?

On Oct 24, 2008, at 11:25 AM, Kenneth McDonald wrote:

I’ve tried using File.expand_path to normalize path names, but this
doesn’t always seem to give identical results for the same file, if
one of the involved paths goes through a symbolic link. Is there
another way to do this?

Thanks,
Ken

Pathname.realpath(pathname)

a @ http://codeforpeople.com/

From the documentation, that seems to compare to different files to
see if they have the same content. Thanks for the suggestion, though.

Looks like realpath will be it, thanks for the suggestions.

Ken

Kenneth McDonald [email protected] wrote:

I’ve tried using File.expand_path to normalize path names, but this
doesn’t always seem to give identical results for the same file, if
one of the involved paths goes through a symbolic link. Is there
another way to do this?

Does Pathname#realpath help? m.

Under Unix, you could do File.stat on each and see if ino and dev are
the same.

On Fri, Oct 24, 2008 at 3:15 PM, Nobuyoshi N. [email protected]
wrote:

Hi,

At Sat, 25 Oct 2008 03:21:21 +0900,
Kenneth McDonald wrote in [ruby-talk:318516]:

From the documentation, that seems to compare to different files to
see if they have the same content. Thanks for the suggestion, though.

It tells whether two files are an identical file, in other
words, same hardlinks. The doc is too terse?

macbook:~ michaelguterl$ ri File::identical

------------------------------------------------------- File::identical?
File.identical?(file_1, file_2) => true or false

 Returns +true+ if the named files are identical.

     open("a", "w") {}
     p File.identical?("a", "a")      #=> true
     p File.identical?("a", "./a")    #=> true
     File.link("a", "b")
     p File.identical?("a", "b")      #=> true
     File.symlink("a", "c")
     p File.identical?("a", "c")      #=> true
     open("d", "w") {}
     p File.identical?("a", "d")      #=> false

Seems pretty concise to me…

Hi,

At Sat, 25 Oct 2008 03:21:21 +0900,
Kenneth McDonald wrote in [ruby-talk:318516]:

From the documentation, that seems to compare to different files to
see if they have the same content. Thanks for the suggestion, though.

It tells whether two files are an identical file, in other
words, same hardlinks. The doc is too terse?

Nobuyoshi N. wrote:

It tells whether two files are an identical file, in other
words, same hardlinks. The doc is too terse?

I think the terminology is misleading. I had thought File.identical?
was a diff test, which is how the word ‘identical’ is normally used with
respect to files. In fact:

$ echo “test” > foo
$ cp foo foo2
$ diff -s foo foo2
Files foo and foo2 are identical

Alternatives might be
same? a, b
same_physical? a, b
very_same? a, b
selfsame? a, b

The closest concept in English is ‘selfsame’, although it is uncommon
especially in spoken English.

  Like two golden birds perched on the selfsame tree
  Intimate friends, the ego and the Self
                        --The Upanishads

Right, my apologies to Nouyoshi. In English, “identical” typically
means “the same in
every detail”, without implying that two things are the same thing.
For example, identical
twins, identical coins, etc.

Now the writer in me will come out :slight_smile: A good description would be
“returns true if the
two paths point to the same file.” same? and same_file_as? might have
been better
alternatives, but it’s too late to worry about that now. :slight_smile:

Thanks for all the help,
Ken

From: Nobuyoshi N. [mailto:[email protected]]

At Sat, 25 Oct 2008 03:21:21 +0900,

Kenneth McDonald wrote in [ruby-talk:318516]:

> From the documentation, that seems to compare to different

files to

> see if they have the same content. Thanks for the

suggestion, though.

It tells whether two files are an identical file, in other

words, same hardlinks. The doc is too terse?

i think, Nobu, the method naming is misleading/clashing in File and
FileUtils.

there is File.identical? but there is also Fileutils#identical? wc is
aliased to compare_file, wc is totally different w File.identical

imho, there are already too many method names in ruby to remember. a lot
almost having the same fxnality.

it would be nice if file.identical just returns nil, or 1, or 0, where
nil for different, 1 for same, and 0 for same self.

comparison of File/fileutils identical follows…

~$ qri file#identical?

 File.identical?(file_1, file_2)   =>  true or false

 Returns true if the named files are identical.

     open("a", "w") {}
     p File.identical?("a", "a")      #=> true
     p File.identical?("a", "./a")    #=> true
     File.link("a", "b")
     p File.identical?("a", "b")      #=> true
     File.symlink("a", "c")
     p File.identical?("a", "c")      #=> true
     open("d", "w") {}
     p File.identical?("a", "d")      #=> false

~$ qri fileutils#identical?
--------------------------------------------------- FileUtils#identical?
identical?(a, b)

 Alias for #compare_file

 Alias for #compare_file

~$ qri fileutils#compare_file
------------------------------------------------- FileUtils#compare_file
compare_file(a, b)

 Returns true if the contents of a file A and a file B are
 identical.

   FileUtils.compare_file('somefile', 'somefile')  #=> true
   FileUtils.compare_file('/bin/cp', '/bin/mv')    #=> maybe false
 (also known as identical?, cmp)

Hi,

At Sat, 25 Oct 2008 14:59:42 +0900,
Peña, Botp [email protected] wrote in [ruby-talk:318545]:

i think, Nobu, the method naming is misleading/clashing in File and FileUtils.

Thank you. We haven’t noticed FileUtils also had had
identical? method when adding the method to File, and it
indeed seems confusing.