Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Where does the *nix system store information about number of hard links to a specific inode? I can't find any information about that. Everywhere what a hard link is but rarely a bit more advanced information that touches inodes related stuff.

An inode stores information about number of links but where does it get it from? Can I locate all the links (both hard and soft) by knowing only the inode number?

share|improve this question

migrated from serverfault.com Jan 27 at 21:51

This question came from our site for professional system and network administrators.

1 Answer 1

up vote 3 down vote accepted

The hard link count is stored in the inode. It starts at 1 when the file is created, increases by 1 each time the link system call is successful, and decreases by 1 each time the unlink system call is successful.

The only way to find all the hard links to the same file, i.e. to find all the pathnames leading to a given inode, is to go through the whole filesystem and compare inode numbers. The inode does not point back to the directory entries.

Directories are a special case: their hard links obey strict rules. (Some unix variants allow root to bypass these rules at the administrator's peril.) The hard links to a directory are its . entry, its children's .. entry, and one entry in its parent directory (the parent being the directory reached by the directory's .. entry).

There is no way to find all the symbolic links pointing to a file. They could be anywhere, including on a filesystem that isn't mounted.

With GNU or FreeBSD find, you can use find /some/dir -samefile /path/to/foo to find all the hard links to the file /path/to/foo that are under /some/dir. With the -L option, you can find all the soft and hard links to that file. You can find an inode by number with the -inum predicate instead of -samefile.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.