Using inode number to locate hardlinks

2

How can I use the inode number to locate all hardlinks to the file "file.txt" in my home directory?

Jurgen Malinao

Posted 2013-03-27T14:10:27.143

Reputation: 99

Answers

1

To find all files in your home directory with inode number N, use either

find ~ -maxdepth 1 -inum N

(home directory only) or

find ~ -xdev -inum N

(includes subdirectories).

The switch -xdev prevents finding files with the same inode number on different file systems, since they're not the hardlinks we're looking for.

To find all hardlinks to file.txt, you can use

find -inum "$(stat -c %i file.txt)"

with or without the -maxdepth switch.

See:

Dennis

Posted 2013-03-27T14:10:27.143

Reputation: 42 934

2I'd add -xdev, for completeness in the general case. By definition, hard links will be on the same file system. (Though in this particular case, home dirs are very unlikely to span filesystems). – Rich Homolka – 2013-03-27T14:25:30.663