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

*2026-11-29*

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.

```sh
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](https://www.gnu.org/software/findutils/manual/find.pdf) · [Apple find source](https://github.com/apple-oss-distributions/shell_cmds/blob/298787009e5432c5e4c378a077f98267077e3495/find/function.c#L1108-L1165)

![A depot worker steps through one of two open doors on the same green streetcar](https://czaixyl0esghkd4s.public.blob.vercel-storage.com/marketing/1abc42ca300fff37ae4decdf484ef40bab4e11cff0463fbf85a966c627a7a3cd.webp)

## Give one file three names

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

```sh
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.

```sh
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.

```text
./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.

```sh
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.

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

## When the link count is higher than your result count

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

```sh
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.

## What changes when you follow symlinks

```sh
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](/blog/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](/catalog/packages/linux-command-line-flashcards/).

*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.*

**Linux Command Line Flashcards: Essential Commands, Flags &amp; Workflows**

Practice essential Linux terminal commands through 100 task\-first cards on paths, files, search, pipes, permissions, processes, archives, and system checks\.

100 cards · Audience languages: English

[Study this deck for free](<https://nibomo.com/catalog/packages/linux-command-line-flashcards/>)

---
*[View the styled HTML version of this page](<https://nibomo.com/blog/find-hard-links-samefile/>)*

*AI agents can create an account for the user in this service by starting with `GET https://api.nibomo.com/v1/`. Once set up, they can also connect over MCP at `https://mcp.nibomo.com/mcp`.*

*Tip: Append `.md` to any URL on https://nibomo.com to get a clean Markdown version of that page.*