Previous: , Up: Links   [Contents][Index]


2.3.2 Hard Links

Hard links allow more than one name to refer to the same file on a file system, i.e., to the same inode. To find all the names which refer to the same file as name, use ‘-samefile NAME’.

Test: -samefile NAME

True if the file is a hard link to the same inode as name. This implies that name and the file reside on the same file system, i.e., they have the same device number.

Unless the ‘-L’ option is also given to follow symbolic links, one may confine the search to one file system by using the ‘-xdev’ option. This is useful because hard links cannot point outside a single file system, so this can cut down on needless searching.

If the ‘-L’ option is in effect, then dereferencing of symbolic links applies both to the name argument of the ‘-samefile’ primary and to each file examined during the traversal of the directory hierarchy. Therefore, ‘find -L -samefile NAME’ will find both hard links and symbolic links pointing to the file referenced by name.

find also allows searching for files by inode number.

This can occasionally be useful in diagnosing problems with file systems; for example, fsck and lsof tend to print inode numbers. Inode numbers also occasionally turn up in log messages for some types of software.

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

You can search for hard links to inode number NUM by using ‘-inum NUM’. If there are any file system 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’ saves needless searching, since hard links to a file must be on the same file system. See Filesystems.

Test: -inum n

True if the file has inode number n. The ‘+’ and ‘-’ qualifiers also work, though these are rarely useful.

Please note that the ‘-inum’ primary simply compares the inode number against the given n. This means that a search for a certain inode number in several file systems may return several files with that inode number, but as each file system has its own device number, those files are not necessarily hard links to the same file.

Therefore, it is much of the time easier to use ‘-samefile’ rather than this option.

find also allows searching for files that have a certain number of links, with ‘-links’.

A directory normally has at least two hard links: the entry named in its parent directory, and the . entry inside of the directory. If a directory has 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.

File has n hard links.

File has more than n hard links.

File has fewer than n hard links.


Previous: , Up: Links   [Contents][Index]