Operating system identifies a file with its inode number. Hard link is pointed to inode number. When two files are created with same inode number, operating system identifies this as a single file. But when more links are created a paramter called link count gets increased.
~]$ du -sch test1
1.0M test1
1.0M total
~]$ ln test1 hardlinktest1
~]$ du -sch test1 hardlinktest1
1.0M test1
1.0M total
~]$ cp -pr test1 copyoftest1
~]$ du -sch test1 hardlinktest1 copyoftest1
1.0M test1
1.0M copyoftest1
2.0M total
~]$
Number of links for a file can be obtained from stat file.
~]$ stat copyoftest1 | grep Links
Device: cbb0b651h/3417355857d Inode: 142898 Links: 1
~]$
~]$ stat test1 | grep Links
Device: cbb0b651h/3417355857d Inode: 142686 Links: 2
~]$
~]$ stat test1 | grep Links
Device: cbb0b651h/3417355857d Inode: 142686 Links: 3
~]$
So, for a hard link, the file will delete when the link count is zero.
Note: An inode contains following information's.
– File type and access rights
– Owner and group identifiers
– File length in bytes
– Number of data blocks in the file
– Various timestamp attributes
– An array of (usually 15) data block pointers
~]$ du -sch test1
1.0M test1
1.0M total
~]$ ln test1 hardlinktest1
~]$ du -sch test1 hardlinktest1
1.0M test1
1.0M total
~]$ cp -pr test1 copyoftest1
~]$ du -sch test1 hardlinktest1 copyoftest1
1.0M test1
1.0M copyoftest1
2.0M total
~]$
Number of links for a file can be obtained from stat file.
~]$ stat copyoftest1 | grep Links
Device: cbb0b651h/3417355857d Inode: 142898 Links: 1
~]$
~]$ stat test1 | grep Links
Device: cbb0b651h/3417355857d Inode: 142686 Links: 2
~]$
~]$ stat test1 | grep Links
Device: cbb0b651h/3417355857d Inode: 142686 Links: 3
~]$
So, for a hard link, the file will delete when the link count is zero.
Note: An inode contains following information's.
– File type and access rights
– Owner and group identifiers
– File length in bytes
– Number of data blocks in the file
– Various timestamp attributes
– An array of (usually 15) data block pointers