开发者

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

This questio开发者_开发问答n already has answers here: Closed 12 years ago.

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.


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.


Use stat or fstat. The stat structure they fill out contains the inode number. If the two are connected, the st_ino fields should have the same value.

Since inode numbers are only unique to a device you'll also need to check the device ID (st_dev).


The stat() or fstat() function will return a structure containing an st_nlink field that states how many hard links to that file exist. I imagine you could then compare the inodes of two different paths, as a hard link should reuse the same inode.


You can also use

$ ls -i

It lists the inode number, which is a definitive unique ID for a file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜