How to check two file paths are the same?

2,607 views
Skip to first unread message

Hiroaki Nakamura

unread,
Mar 26, 2016, 10:59:46 AM3/26/16
to golang-nuts
Hi all,

I like to compare two file paths and see if they are the same.
A one idea came to my mind is call filepath.Abs and compare the results.

However https://golang.org/pkg/path/filepath/#Abs says
> The absolute path name for a given file is not guaranteed to be unique.

Could you tell me an idiomatic way to compare two file paths?

Thanks,
Hiroaki

Peter Weinberger

unread,
Mar 26, 2016, 11:35:50 AM3/26/16
to Hiroaki Nakamura, golang-nuts
It depends on operating system details. In systems with hard (or symbolic) links, a file can be at the end of multiple paths. Looking at the result of the Sys() method of os.FileInfo should help, but even its type depends on the system. Go doesn't provide a system-independent way of doing it.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Jones

unread,
Mar 26, 2016, 11:59:15 AM3/26/16
to Peter Weinberger, Hiroaki Nakamura, golang-nuts
Hiroaki,

Peter Weinberger has given you the deep answer based on the presumption that when you ask about comparing file paths you actually mean ‘do they represent the same file.’

If instead you mean, ‘removing dot and dot-dot or perhaps tilde-username or on some filesystems removing upper/lower case from consideration, are the two final strings representing the file paths the same’ then absolute path is good. This is what you literally asked but likely not what you mean.

One slow but sure system-independent approach to the absolute answer is to go to the final directory in each path (the last part if that is a directory or the enclosing directory if the last part is not a directory) and trace the path upward to the filesystem root. if the paths upward are identical, then so are the files (if the filenames are match in the case that the final parts are not directories). (In olden times, you could just get each file’s inode and compare.)

— 
Michael Jones, CEO  •  mic...@wearality.com  •  +1 650 656-6989 
Wearality Corporation  •  289 S. San Antonio Road  •  Los Altos, CA 94022

Hiroaki Nakamura

unread,
Mar 26, 2016, 12:18:41 PM3/26/16
to Michael Jones, Peter Weinberger, golang-nuts
Peter and Michael,

Thanks for your deep answers.

I was experimenting with https://godoc.org/gopkg.in/fsnotify.v1 and
wanted to check whether the relative path
in Event.Name is the file I am interested in. So in this case the
absolute path is good, right?

Good to know that it is not easy to compare file paths to see they
represent the same file, though.
So thanks again!

roger peppe

unread,
Mar 26, 2016, 1:18:42 PM3/26/16
to Hiroaki Nakamura, golang-nuts
The standard way is to stat them and use os.SameFile.

Hiroaki Nakamura

unread,
Mar 26, 2016, 2:04:41 PM3/26/16
to roger peppe, golang-nuts
Hi roger,

Oh this is easy.
Thanks for a nice info!
Reply all
Reply to author
Forward
0 new messages