How can I find all hardlinked files on a filesystem?
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'
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.