#!/usr/bin/perl use strict; use warnings; $/=""; while (<>) { chomp; my %dups; # hash: inode -> array of filenames foreach (split /\n/) { my $inode = (stat)[1]; push @{$dups{$inode}}, $_; } my @keys = keys(%dups); # all are hardlinks to the same data next if (scalar @keys < 2); # sort by nlink @keys = sort { scalar $dups{$a} <=> scalar $dups{$b} } @keys; my $target = $dups{shift @keys}[0]; foreach (@keys) { foreach (@{$dups{$_}}) { print "relinking $_ -> $target\n"; unlink($_); link($target, $_) or die "link failed"; } } }