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

K

Kenneth McDonald

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
 
N

Nobuyoshi Nakada

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?
 
M

matt neuburg

Kenneth McDonald said:
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.
 
A

ara.t.howard

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/
 
K

Kenneth McDonald

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


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?
 
N

Nobuyoshi Nakada

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?
 
B

Brian Candler

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

Michael Guterl

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...
 
T

The Higgs bozo

Nobuyoshi said:
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
 
K

Kenneth McDonald

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
 
P

Peña, Botp

From: Nobuyoshi Nakada [mailto:[email protected]]=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)
 
N

Nobuyoshi Nakada

Hi,

At Sat, 25 Oct 2008 14:59:42 +0900,
Pe=F1a said:
i think, Nobu, the method naming is misleading/clashing in File and FileU=
tils.

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

--=20
Nobu Nakada
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top