Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need to find all hardlinked files on a given filesystem. E.g. get a list of files, each line contains linked pairs, or triplets, etc.

I understand more or less how to do it, one needs to create a dictionary keyed by inode for all files/directories on a filesystem, exclude "." and ".." links, and then indodes with more than one name are hardlinks... But I hope that maybe a ready-made solution exists, or someone already wrote such a script.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

You can run the following command :

find / -type f -printf '%n %p\n' | awk '$1 > 1{$1="";print}'

to find all hard-linked files.

share|improve this answer
1  
Thanks, this is not exactly what I wanted, but close enough. I can add '%i' to print the inode numbers and then sort/group by it... –  haimg Oct 10 '12 at 15:59
find . -type f -links +1 2>/dev/null

gives a list of all files which have more than one link, i.e. files to which there exists a hard link. Looping over this is then relatively easy – a hacky solution if you don’t have that many files would be

for i in $(find . -type f -links +1 2>/dev/null); do find -samefile $i | awk '{printf "%s ", $1}'; printf "\n"; done | sort | uniq

But I sincerely hope that there are better solutions, for example by letting the first find call print inode numbers and then using find’s -inum option to show all files associated with this inode.

share|improve this answer
    
Ouch! This scans the filesystem again and again for each hardlinked file... –  haimg Oct 10 '12 at 16:01
    
I didn’t claim it was fast – and it sort-of-works for small directory trees. Of course, a proper index, that could be built from, for example, the output of find . -type f -printf '%i %p\n', would allow one to build a much faster solution. –  Claudius Oct 10 '12 at 16:08
    
And that don't handle space in path AFAIK. –  sputnick Oct 10 '12 at 16:12
    
For the for loop, adjusting IFS accordingly would work. To parse the output of the find command in my comment, declaring everything between the first space and the end of the line to be the filename should work, too. –  Claudius Oct 10 '12 at 16:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.