How to Effectively Trace Hardlink in Linux

findhard linkinode

Softlinks are easily traceable to the original file with readlink etc… but I am having a hard time tracing hardlinks to the original file.

$ ll -i /usr/bin/bash /bin/bash  
1310813 -rwxr-xr-x 1 root root 1183448 Jun 18 21:14 /bin/bash*  
1310813 -rwxr-xr-x 1 root root 1183448 Jun 18 21:14 /usr/bin/bash*
                   ^

above is as expected – cool –> both files point to same inode 1310813
(but the number of links, indicated by ^, shows to be 1. From Gilles answer the reason for this can be understood)

$ find / -samefile /bin/bash 2>/dev/null
/usr/bin/bash

above is as expected – so no problems.

$ find / -samefile /usr/bin/bash 2>/dev/null
/usr/bin/bash  

above is NOT cool. How do I trace the original file or every hardlink using the /usr/bin/bash file as reference?

Strange – below did not help either.

$ find / -inum 1310813 2>/dev/null
/usr/bin/bash

Best Answer

First, there is no original file in the case of hard links; all hard links are equal.

However, hard links aren’t involved here, as indicated by the link count of 1 in ls -l’s output:

$ ll -i /usr/bin/bash /bin/bash  
1310813 -rwxr-xr-x 1 root root 1183448 Jun 18 21:14 /bin/bash*  
1310813 -rwxr-xr-x 1 root root 1183448 Jun 18 21:14 /usr/bin/bash*

Your problem arises because of a symlink, the bin symlink which points to usr/bin. To find all the paths in which bash is available, you need to tell find to follow symlinks, using the -L option:

$ find -L / -xdev -samefile /usr/bin/bash 2>/dev/null
/usr/bin/rbash
/usr/bin/bash
/bin/rbash
/bin/bash

I’m using -xdev here because I know your system is installed on a single file system; this avoids descending into /dev, /proc, /run, /sys etc.

Related Question