Hardlinks in Linux: 3 Commands to Find Duplicate Files

Quick Linux Tip #111:

Question: I have duplicate-looking files. Are they hardlinks or real copies?

Try: find /home/linuxteck -type f -links +1 -printf '%i %n %p\n' | sort

Info: Finds files sharing inodes and link counts to identify hardlinks.

Examples:

  • $ find /path -samefile /path/to/original  # Locate all hardlinks referencing a specific file
  • $ ls -li file.txt  # Display file inode numbers in the first column
  • $ stat -c '%h %n' file.txt  # Output hardlink count and filename

Note: Hardlinks share the same inode and disk blocks without consuming extra space. Changes reflect across all links, and deleting one link leaves others intact. Note that hardlinks cannot cross filesystem boundaries.




LinuxTeck.com
linuxteck:~$ find /home/linuxteck -type f -links +1 -printf '%i %n %p\n' | sort
524290 2 /home/linuxteck/documents/report.pdf
524290 2 /home/linuxteck/backup/report.pdf
524301 3 /home/linuxteck/photos/vacation.jpg
524301 3 /home/linuxteck/share/vacation.jpg
524301 3 /home/linuxteck/backup/vacation.jpg
524320 2 /home/linuxteck/scripts/backup.sh
524320 2 /usr/local/bin/backup.sh
linuxteck:~$

PREVIOUS ARTICLE Quick Linux Tip #110: Linux LUKS Keyfile: 4 Steps to Automate Disk Unlocking NEXT ARTICLE Quick Linux Tip #112: Chroot in Linux: 3 Powerful Ways to Isolate Processes
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.