How to Find Hard Links to a File with find -samefile

If ls -l shows a link count of 3 for reports/current.txt, that file has three names. Run this to look for those names under your current directory, starting on the same filesystem as the file.

find -P . -xdev -type f -samefile reports/current.txt -print

Replace reports/current.txt with the path to a regular file you already know. -P tells find to leave symlinks unfollowed, and -type f selects regular files. The results include the reference file's path if find reaches it.

-xdev keeps find from descending into other filesystems from its starting directory, here .. That starting directory matters because the native macOS find tested here checks only the inode number, which different filesystems can reuse. Starting on the wrong filesystem can produce a false match even with -xdev. GNU find checks the device number and inode number together. GNU Findutils manual, §2.4.2 · Apple find source

A depot worker steps through one of two open doors on the same green streetcar

Give one file three names

Run this wherever you can create a fresh directory. If samefile-lab already exists there, choose a different location.

mkdir samefile-lab && cd samefile-lab

Once you're in samefile-lab, create a file, two more hard links to it, a copy, and a symlink.

mkdir reports archive
printf 'April inventory\n' > reports/current.txt
ln reports/current.txt archive/april.txt
ln reports/current.txt extra.txt
cp reports/current.txt reports/copy.txt
ln -s reports/current.txt shortcut.txt

Run the opening find command from samefile-lab. It prints these three paths, sorted here for readability.

./archive/april.txt
./extra.txt
./reports/current.txt

Use ls -li to see the inode and link count, then use cmp to check the copy's contents.

ls -li reports/current.txt archive/april.txt extra.txt reports/copy.txt
cmp reports/current.txt reports/copy.txt

The inode number comes first. The link count is the number after the permissions. current.txt, april.txt, and extra.txt share an inode number and each shows a link count of 3. The copy has its own inode and a link count of 1. cmp exits successfully with no output because the bytes match. -samefile tests file identity, so the copy stays out of the results.

On macOS, you can also use stat to verify a match if your search spans volumes. This prints the device number, inode number, and path. Both numbers match for these names in the lab.

stat -f '%d %i %N' reports/current.txt archive/april.txt

From samefile-lab, search just ./archive. The reference path reports/current.txt is still relative to the directory where you run the command.

find -P ./archive -type f -samefile reports/current.txt -print

This prints only ./archive/april.txt. The link count stays at 3 because the other two names are still there, outside archive. If a real search comes up short, check for permission errors and widen the search to a readable directory on the same filesystem.

find -L . -type f -samefile reports/current.txt -print

This adds ./shortcut.txt to the three paths from the opening search. -L makes find examine the symlink's target, reports/current.txt. The link count stays at 3 because shortcut.txt is a symlink. To see how -L also affects which directories the search enters, read Make find Follow Symlinks.

Try it again from memory

In a few days, write the command that finds all three hard links. Predict which paths it will print, then predict what changes when you start at ./archive or switch to -L. For spaced review of ln, ln -s, and find, use the Linux command-line flashcard deck.

AI assisted with research, drafting, and editing. AI agents ran the lab with native /usr/bin/find on macOS 26.6.1 arm64 and GNU Findutils 4.10.0 on Debian 13.7. The cover illustration is AI-generated.

Read next