How can I find all hardlinked files on a filesystem?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Thinking It Over
--
Chapters
00:00 How Can I Find All Hardlinked Files On A Filesystem?
00:30 Accepted Answer Score 35
00:47 Answer 2 Score 18
01:19 Answer 3 Score 1
01:51 Thank you
--
Full question
https://superuser.com/questions/485919/h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#linux #hardlink
#avk47
ACCEPTED ANSWER
Score 35
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'
ANSWER 2
Score 18
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.
ANSWER 3
Score 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