Skip to content

Instantly share code, notes, and snippets.

@simonw
Created November 8, 2009 09:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save simonw/229186 to your computer and use it in GitHub Desktop.
Save simonw/229186 to your computer and use it in GitHub Desktop.
Python script for telling if two files are hard links to the same thing
#!/usr/bin/env python
"is_hard.py - tell if two files are hard links to the same thing"
import os, sys, stat
def is_hard_link(filename, other):
s1 = os.stat(filename)
s2 = os.stat(other)
return (s1[stat.ST_INO], s1[stat.ST_DEV]) == \
(s2[stat.ST_INO], s2[stat.ST_DEV])
if __name__ == '__main__':
if len(sys.argv) != 3:
print "Two arguments required"
else:
if is_hard_link(sys.argv[1], sys.argv[2]):
print "Hard link confirmed"
else:
print "Those are different files, no hard link"
@bgusach
Copy link

bgusach commented May 23, 2017

This does not work properly on windows. Returns false positives.

@kandul
Copy link

kandul commented Jan 31, 2018

It works for me (at least on Windows 7)

@dair-targ
Copy link

In Python 3 the is_hard_link could be replaced with os.path.samefile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment