Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are two hardlinks connected to one file in Linux? [duplicate]

Tags:

c

linux

unix

Possible Duplicate:
How to check whether two file names point to the same physical file

How can I know if two hardlinks are connected to one file from C in Linux.

Thanks.

like image 703
Ximik Avatar asked Dec 17 '22 19:12

Ximik


1 Answers

Use the stat() or fstat() function for both paths. If in the returned structures both the st_dev and st_ino fields are identical, then the paths refer to the same filesystem object.

EDIT:

Note that you need to check both st_dev and st_ino. Otherwise you run the risk of matching two files in different filesystems that just happen to have the same inode number. You may be able to see this if you run stat on two mountpoints:

$ stat / /boot | grep Device
Device: 903h/2307d  Inode: 2           Links: 23
Device: 902h/2306d  Inode: 2           Links: 3

You can clearly see the identical inode numbers in the output.

like image 153
thkala Avatar answered Feb 01 '23 17:02

thkala