44

I've created some hard links on my Windows 7 file system using mklink. It was some time ago and I can't remember for sure where, or which files. When I use Explorer, all files look the same. When I use the command line and type "dir", they all look the same.

How do I find hard links? Or how do I determine whether a specific file IS a hard link?

9 Answers 9

44

All files are hard links, with link counts of at least 1.

This is why the files look the same. They are the same. What you seem to be looking for are files where there are more than one link to the file. There's very little that distinguishes a file with a link count greater than one from a file with a link count of one … except the link count (and some odd behaviour with respect to attributes and date stamps).

And that is dead easy to check with the find command that is in Microsoft's Windows Subsystem for Linux: find . -links +1.

p.s. Windows Subsystem for Linux was previously called "Subsystem for UNIX-based Applications" in 2008 for Windows-Server-2008 (for more info read here https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/cc772343(v=ws.11)?redirectedfrom=MSDN ).

6
  • 12
    just as a note, cygwin /bin/find also works. Thanks!
    – arcyqwerty
    Sep 18, 2012 at 2:25
  • 6
    Just to be clear the "Microsoft find" being referenced in the solution here is not the default installed Windows 7. You have to download and install a new Microsoft find. Then you can use that find utility. Or install Cygwin's find command like @arcyqwerty (which will probably have better support long term). Aug 13, 2013 at 14:40
  • 2
    Can also use the built-in fsutil hardlink list filename. (see Sergey's answer)
    – Svish
    Jul 30, 2017 at 16:53
  • Does that mean that both links are equal? Can I delete the original file/link and keep the second one without the memory where it points to is freed (and thus without it being overwritten by new files)?
    – 816-8055
    Jan 12, 2021 at 16:32
  • @TrevorBoydSmith 8 years later... I think the answerer was actually making a recommendation to use WSL (which she/he calls "Subsystem for Unix-based Applications"). No doubt this is better known now than it was in 2011 ... indeed I'm surprised it even existed then. The latest version of this is pretty useful, and the Linux find command will be present, I assume, with any Linux distro available for installation in WSL. Apr 11, 2021 at 14:58
59

On Windows 7 (also works on Windows 10), use command:

fsutil hardlink list MyFileName.txt

It lists all hardlinks to file with name MyFileName.txt.

3
  • 1
    Note that you need to be admin to run fsutil. Aug 18, 2016 at 17:54
  • 4
    @AlastairMaw On at least Windows 10 you don't need to be an admin to run that particular command. Or does it work to be admin without UAC? (that is, just the group without actual elevation) Mar 12, 2018 at 22:20
  • You saved my day!
    – Anduin Xue
    May 25, 2021 at 7:50
15

Unfortunately, there is no way for the OS to find all your hardlinks without looking at each file.

For Explorer, you can download the very handy Link Shell Extension, which overlays files with hardlinks with a red shortcut-like arrow.

It also makes it rather effortless to create hardlinks, symlinks and junctions through Explorer's context menu.

Bonus chatter:

Technically, all files are hardlinks. Thus, you are really looking for files with more than one hardlink.

2
  • 2
    Actually, it is. See Sergey's answer.
    – Svish
    Jul 30, 2017 at 16:51
  • 3
    Sergey's answer works for the second part of the question, but not the first.
    – surfasb
    Jul 30, 2017 at 21:44
4

use finddupe:

finddupe -listlink c:\photos
1
  • 1
    -listlink Puts finddupe in hardlink finding mode. In this mode, finddupe will list which groups of files are hardlinked together. All hardlinked instances found of a file are shown together. However, finddupe can only find instances of the hardlinked file that are within the search path.
    – Shayan
    Jan 3, 2019 at 13:51
2

 A simple way for one-off checks: Are file attributes changing together?

If you want to test whether the files A and B are hard links to the same content and you do not want to install or run anything, open the Properties window of file A and toggle for example its Read-only attribute. Now open Properties window of file B to see whether change of the attribute (of file A) took place also here. If yes, then the answer is positive.

Why this works? Because related hard links share the same set of file attributes and time stamps.

This having said, the same test can be made with time stamps of files, but they cannot be changed as easily as Read-only flag in order to perform the check.

2

I wrote a program that searches an directory, optionally recursive, for any kind of link. It can output to stdout or as a tab separated table (tsv) for reading in Excel/Libreoffice.

Source code can be found on my Github repo JavaScriptDude/find_links

find_links - Finds any kind of link in windows 
Usage: find_links -p <path> [-r] [-x] [-t] [-f <filter>]
-path | -p: Path to search (required)
-recurse | -r: recursive search
-exec | -x: Open tsv using explorer
-tsv | -t: Output in tsv format (tab separated)
-filter | -f: Filter the target using optional wildcards Eg: *foo*
1
  • That looks exceptionally useful! Thank you for writing it and posting a description here! May 4, 2021 at 6:41
2

There is no need to download anything.
You can CD to desired folder and do a recursive search:

echo off & for /F "delims=" %A in ('dir /s /b') do echo. & fsutil hardlink list "%A"

A single isolated line means it's a normal file
Two adjacent lines means it's a HardLink.

1
  • 2
    You can test it out in "C:\Program Files\Common Files\System"
    – mjoao
    Aug 10, 2021 at 11:32
1

This works in Windows 10 as well as 11, no downloads required. Basic idea, use command prompt (elevated recommended) Navigate to the folder you want to check. Execute this command to list ALL FILES in the folder and subfolder, so don't do a huge drive all at once. As noted below, hardlinked files will appear twice.

echo off & for /F "delims=" %A in ('dir /s /b') do echo. & fsutil hardlink list "%A" > "%UserProfile%\Desktop\HardLinks.txt"

A single isolated line means the it's a normal file. You can ignore this. Two lines together means it's a hard link. The top path will be for the hard link, and the bottom path will be for it's target source.

The first part of the article discusses listing Symbolic Links and Junction Points.

Reference: https://www.elevenforum.com/t/find-all-symbolic-links-and-junction-points-in-windows.4225/

I'm busy with taxes at the moment, but I feel like it would be fairly straightforward to write a simple script to remove the duplicate listings and only output the hard links. Maybe I'll ask my son.

1
  • According to my reference, this should work with Windows 7/8/8.1 too. Feb 14, 2023 at 4:36
-1

Powershell:

gi C:\windows\notepad.exe | % { $_.LinkType; $_.Target }

The LinkType field is set to "HardLink" if there is more than one hard link (normal files have 1) and Target lists the other targets.

2
  • Please improve your answer with an explanation for future visitors Jul 13, 2023 at 12:57
  • Somehow this doesn't work for all hard links. The ones I created myself do not have LinkType set to HardLink.
    – N.I.
    Sep 11, 2023 at 7:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .