Previous: Symbolic Links, Up: Links


2.2.2 Hard Links

Hard links allow more than one name to refer to the same file. To find all the names which refer to the same file as NAME, use ‘-samefile NAME’. If you are not using the ‘-L’ option, you can confine your search to one filesystem using the ‘-xdev’ option. This is useful because hard links cannot point outside a single filesystem, so this can cut down on needless searching.

If the ‘-L’ option is in effect, and NAME is in fact a symbolic link, the symbolic link will be dereferenced. Hence you are searching for other links (hard or symbolic) to the file pointed to by NAME. If ‘-L’ is in effect but NAME is not itself a symbolic link, other symbolic links to the file NAME will be matched.

You can also search for files by inode number. This can occasionally be useful in diagnosing problems with filesystems for example, because fsck tends to print inode numbers. Inode numbers also occasionally turn up in log messages for some types of software, and are used to support the ftok() library function.

You can learn a file's inode number and the number of links to it by running ‘ls -li’ or ‘find -ls’.

You can search for hard links to inode number NUM by using ‘-inum NUM’. If there are any filesystem mount points below the directory where you are starting the search, use the ‘-xdev’ option unless you are also using the ‘-L’ option. Using ‘-xdev’ this saves needless searching, since hard links to a file must be on the same filesystem. See Filesystems.

— Test: -samefile NAME

File is a hard link to the same inode as NAME. If the ‘-L’ option is in effect, symbolic links to the same file as NAME points to are also matched.

— Test: -inum n

File has inode number n. The ‘+’ and ‘-’ qualifiers also work, though these are rarely useful. Much of the time it is easier to use ‘-samefile’ rather than this option.

You can also search for files that have a certain number of links, with ‘-links’. Directories normally have at least two hard links; their . entry is the second one. If they have subdirectories, each of those also has a hard link called .. to its parent directory. The . and .. directory entries are not normally searched unless they are mentioned on the find command line.

— Test: -links n

File has n hard links.

— Test: -links +n

File has more than n hard links.

— Test: -links -n

File has fewer than n hard links.