Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 7 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
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
You can avoid the need for awk by using find's "-links +n' syntax. e.g. to find all files with at least two links and print out the necessary info: find / -type f -links +1 -printf '%i %n %p\n' – mbafford Jan 16 '15 at 14:50
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. – Gilles Quenot 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
@Claudius 99.9999% of he time, changing $IFS is far more trouble than it's worth. For something as simply as handling filenames with spaces, there's an easier solution. Of course, you could also have filenames with $'\n' or $'\t' in them, so really those find commands should be using find -print0 and xargs -0, but that's a malicious edge case most users probably don't care about. – Parthian Shot Feb 24 at 21:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .