在Linux中兩個硬鏈接連接到一個文件嗎?(復制)

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


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.

如何知道兩個硬鏈接在Linux中是否連接到一個來自C的文件。

Thanks.

謝謝。

4 个解决方案

#1


3  

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.

使用屬性或函數。他們填寫的統計結構包含inode號。如果這兩個字段連接在一起,那么st_ino字段應該具有相同的值。

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

由於inode號只對設備是唯一的,您還需要檢查設備ID (st_dev)。

#2


7  

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.

對於這兩條路徑使用stat()或fstat()函數。如果在返回的結構中,st_dev和st_ino字段都是相同的,那么路徑引用相同的文件系統對象。

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:

注意,您需要同時檢查st_dev和st_ino。否則,您將面臨在不同的文件系統中匹配兩個恰好具有相同inode號的文件的風險。如果您在兩個掛載點上運行stat,您可能會看到:

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

您可以在輸出中清楚地看到相同的inode號。

#3


2  

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.

stat()或fstat()函數將返回一個包含st_nlink字段的結構,該結構說明該文件存在多少硬鏈接。我想您可以比較兩種不同路徑的inode,因為硬鏈接應該重用相同的inode。

#4


0  

You can also use

您還可以使用

$ ls -i

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

它列出了inode號,這是文件的唯一標識。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/12/05/72998cbc095033a14cdb2ad66ed599fd.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com