Hard links in Windows NTFS file system are multiple directory entries that reference the same file data on disk. Unlike shortcuts or junctions, hard links are true file system references that maintain file integrity even when the original reference is deleted. This feature is particularly useful for developers managing versioned files or creating efficient storage solutions.
The most reliable way to find hard links is through the command prompt using built-in Windows utilities:
Using fsutil
fsutil hardlink list "C:\\path\\to\\yourfile.ext"
This command will output all hard links pointing to the same file data. For example:
C:\\Program Files\\App\\config.xml C:\\Users\\Admin\\Documents\\backup_config.xml
PowerShell Alternative
For more advanced processing, use this PowerShell script:
Get-ChildItem -Path "C:\\target" -Recurse | Where-Object { (fsutil hardlink list $_.FullName).Count -gt 1 } | Select-Object FullName
While Windows Explorer doesn't natively display hard link information, these third-party tools can help:
- Link Shell Extension: Adds context menu options to create and view hard links
- WinDirStat: Shows file storage usage including hard link relationships
- Process Explorer: Displays file handles including hard link references
Consider a scenario where you need to maintain identical header files across multiple projects:
mklink /H "C:\\ProjectA\\headers\\common.h" "C:\\Shared\\common.h" mklink /H "C:\\ProjectB\\includes\\common.h" "C:\\Shared\\common.h"
To later verify all references:
fsutil hardlink list "C:\\Shared\\common.h"
When working with hard links, developers often encounter:
- Permission errors (run commands as Administrator)
- Path length limitations (use short paths with ~ notation)
- Network share incompatibility (hard links work only on local NTFS volumes)
Hard links in Windows NTFS file system allow multiple directory entries to reference the same physical file data. Unlike symbolic links, hard links are direct references to the file's inode (MFT record in NTFS terms). When you create a hard link, you're essentially adding another name that points to the same data blocks on disk.
Windows provides the built-in fsutil
command to work with hard links. To enumerate all hard links for a specific file:
fsutil hardlink list "C:\\path\\to\\yourfile.ext"
Example output might show:
\\?\C:\Folder1\file.txt
\\?\C:\Folder2\linkedfile.txt
For more advanced handling, PowerShell can access the same functionality:
Get-Item "C:\path\to\file.txt" | Select-Object -ExpandProperty HardLinks
Or to get all hard links recursively in a directory:
Get-ChildItem -Recurse | Where-Object { $_.HardLinks.Count -gt 1 }
For developers needing to check hard links programmatically, the Windows API provides several options:
#include <windows.h>
#include <stdio.h>
void EnumerateHardLinks(LPCWSTR filename) {
HANDLE hFile = CreateFileW(filename, 0,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
wprintf(L"Failed to open file. Error: %d\n", GetLastError());
return;
}
BY_HANDLE_FILE_INFORMATION fileInfo;
if (!GetFileInformationByHandle(hFile, &fileInfo)) {
wprintf(L"GetFileInformationByHandle failed. Error: %d\n", GetLastError());
CloseHandle(hFile);
return;
}
// Number of hard links is in fileInfo.nNumberOfLinks
wprintf(L"Total hard links: %d\n", fileInfo.nNumberOfLinks);
CloseHandle(hFile);
}
For GUI-based solutions, consider these tools:
- Link Shell Extension - adds Explorer context menu options
- Process Explorer - can show hard link information in its file properties dialog
- WinDirStat - visualizes disk usage including hard links
When working with hard links:
- Deleting a hard link doesn't delete the file data until the last link is removed
- Hard links only work within the same volume
- Windows doesn't track which link was created first
- File attributes are shared across all hard links