How can I find all hardlinked files on a filesystem?

22

9

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.

haimg

Posted 2012-10-10T15:35:24.747

Reputation: 19 503

Answers

18

You can run the following command :

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

to find all hard-linked files.

Or @mbafford version:

find / -type f -links +1 -printf '%i %n %p\n'

Gilles Quenot

Posted 2012-10-10T15:35:24.747

Reputation: 3 111

15You 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 – 2015-01-16T14:50:47.157

how about piping through sort (+ uniq)? i was curious so gave it a go on my main computer (16GB i5-2500k with ssd). with 2187757 files (find / -xdev -type f | wc) takes 12 real secs when returning 3820 files / 570 inodes (time sudo find / -xdev -type f -links +1 -printf "%i\n" | sort | uniq | wc). you would need to include the %n %p for the actual files as i took them out for counting inodes. – northern-bradley – 2018-10-24T09:45:36.813

1Thanks, 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 – 2012-10-10T15:59:05.093

16

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.

Claudius

Posted 2012-10-10T15:35:24.747

Reputation: 6 330

@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 – 2016-02-24T21:22:36.563

@Claudius, what does 2>/dev/null do in the command above? – Sati – 2018-05-05T06:48:08.927

1@Sati: it ensures that error messages are discarded (e.g. for folders you haven't access to like lost+found etc.); which is especially important, if the output should be processed further like in the second line. – DJCrashdummy – 2018-10-01T18:40:00.260

1Ouch! This scans the filesystem again and again for each hardlinked file... – haimg – 2012-10-10T16:01:01.127

1I 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 – 2012-10-10T16:08:03.547

And that don't handle space in path AFAIK. – Gilles Quenot – 2012-10-10T16:12:21.513

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 – 2012-10-10T16:13:59.190

1

IMHO the best way is to use the following line (for sure you have to replace /PATH/FOR/SEARCH/ with whatever you want to search):

find /PATH/FOR/SEARCH/ -xdev -printf '%i\t%n\t%p\n' | fgrep -f <(find . -xdev -printf '%i\n' | sort -n | uniq -d) | sort -n

this scans the filesystem only once, shows inode, number of hardlinks and path of files with more than one hardlink and sorts them according to the inode.

if you are annoyed by error messages for folders you aren't allowed to read, you can expand the line to this:

find /PATH/FOR/SEARCH/ -xdev -printf '%i\t%n\t%p\n' 2> /dev/null | fgrep -f <(find . -xdev -printf '%i\n' 2> /dev/null | sort -n | uniq -d) | sort -n

DJCrashdummy

Posted 2012-10-10T15:35:24.747

Reputation: 163