[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Kenneth McDonald

10/24/2008 5:26:00 PM

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


11 Answers

Nobuyoshi Nakada

10/24/2008 5:34:00 PM

0

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?

--
Nobu Nakada

matt

10/24/2008 5:35:00 PM

0

Kenneth McDonald <kenneth.m.mcdonald@sbcglobal.net> 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.


--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

ara.t.howard

10/24/2008 5:36:00 PM

0


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://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Kenneth McDonald

10/24/2008 6:21:00 PM

0

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


On Oct 24, 2008, at 12:34 PM, Nobuyoshi Nakada wrote:

> 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?
>
> --
> Nobu Nakada
>


Nobuyoshi Nakada

10/24/2008 7:15:00 PM

0

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?

--
Nobu Nakada

Brian Candler

10/24/2008 7:17:00 PM

0

Under Unix, you could do File.stat on each and see if ino and dev are
the same.
--
Posted via http://www.ruby-....

Michael Guterl

10/24/2008 7:31:00 PM

0

On Fri, Oct 24, 2008 at 3:15 PM, Nobuyoshi Nakada <nobu@ruby-lang.org> 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 Higgs bozo

10/24/2008 8:41:00 PM

0

Nobuyoshi Nakada 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
--
Posted via http://www.ruby-....

Kenneth McDonald

10/24/2008 11:42:00 PM

0

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


On Oct 24, 2008, at 3:41 PM, The Higgs bozo wrote:

> Nobuyoshi Nakada 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
> --
> Posted via http://www.ruby-....
>


Peña, Botp

10/25/2008 6:00:00 AM

0

From: Nobuyoshi Nakada [mailto:nobu@ruby-lang.org]=20
# 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=20
# files to =20
# > see if they have the same content. Thanks for the=20
# suggestion, though.
#=20
# 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?
-------------------------------------------------------=20
File.identical?(file_1, file_2) =3D> true or false
--------------------------------------------------------
Returns true if the named files are identical.

open("a", "w") {}
p File.identical?("a", "a") #=3D> true
p File.identical?("a", "./a") #=3D> true
File.link("a", "b")
p File.identical?("a", "b") #=3D> true
File.symlink("a", "c")
p File.identical?("a", "c") #=3D> true
open("d", "w") {}
p File.identical?("a", "d") #=3D> 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') #=3D> true
FileUtils.compare_file('/bin/cp', '/bin/mv') #=3D> maybe false
(also known as identical?, cmp)