Forum: Ruby How to tell if two paths point to the same file?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kenneth McDonald (Guest)
on 2008-10-24 21:26
(Received via mailing list)
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
Nobuyoshi N. (Guest)
on 2008-10-24 21:34
(Received via mailing list)
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?
Ara H. (Guest)
on 2008-10-24 21:36
(Received via mailing list)
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/
matt neuburg (Guest)
on 2008-10-24 21:40
(Received via mailing list)
Kenneth McDonald <removed_email_address@domain.invalid> 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.
Kenneth McDonald (Guest)
on 2008-10-24 22:23
(Received via mailing list)
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
Nobuyoshi N. (Guest)
on 2008-10-24 23:15
(Received via mailing list)
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?
Brian C. (Guest)
on 2008-10-24 23:17
Under Unix, you could do File.stat on each and see if ino and dev are
the same.
Michael G. (Guest)
on 2008-10-24 23:31
(Received via mailing list)
On Fri, Oct 24, 2008 at 3:15 PM, Nobuyoshi N. 
<removed_email_address@domain.invalid>
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...
The H. (Guest)
on 2008-10-25 00:41
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
Kenneth McDonald (Guest)
on 2008-10-25 03:42
(Received via mailing list)
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 :-) 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. :-)

Thanks for all the help,
Ken
Peña, Botp (Guest)
on 2008-10-25 10:00
(Received via mailing list)
From: Nobuyoshi N. [mailto:removed_email_address@domain.invalid]
# 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)
Nobuyoshi N. (Guest)
on 2008-10-25 12:22
(Received via mailing list)
Hi,

At Sat, 25 Oct 2008 14:59:42 +0900,
Peña, Botp <removed_email_address@domain.invalid> 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.
This topic is locked and can not be replied to.