Tuesday, November 22, 2011

Find all Hard Links to a File

While writing a wrapper to submit files for batch processing I found a need to find all the hard links to a file. Like so many things in Linux, the solution turns out to be simple -- once I found it.

Use the find command

$ find <dir to start search in> -xdev -samefile <path to file>

For example, to find all the hard link to the file /home/buser/chosen.one.txt on the home partition, use:

$ find /home -xdev -samefile /home/buser/chosen.one.txt

The switch -xdev means to only search on the original volume. It can be omitted but, because a hard link cannot cross volume boundaries, the switch will save time by not searching where a linked file cannot be anyways.

0 comments:

Post a Comment

Off topic comments will be deleted. Comments with spelling or grammar errors may be deleted unless they have hoplophobic or statist content in which case they will be highlighted.