3

How to find all the hard and symbolic links to a file/folder in Windows (using standard OS functions)?

What is opposed to UNIX?

Artyom Ionash
  • 405
  • 7
  • 17

2 Answers2

2

From this nice tutorial for linux:

https://jackal.livejournal.com/2164247.html

Get the inode number:

ls -i FILENAME

Find file by inode number:

find / -inum INODE-NUM-FROM-ABOVE

Optionally restrict find to speed up the search.

Example:

> ls -i myfile.csv 
74714625 myfile.csv

> find ~/myfolder/ -inum 74714625
/home/me/myfolder/myfile.csv
/home/me/myfolder/another/path/another_filename_with_same_inode.csv
David Parks
  • 30,789
  • 47
  • 185
  • 328
0

Symlinks:

You´ll have to search all files on all hard disks etc.etc. and check each of them individually. Otherwise it´s not possible to get a list of symlinks for a specific file.

For Linux, How do you determine using stat() whether a file is a symbolic link? may help.

For Windows ... well, Reparse points are not explainable in one line.
It´s not trivial to check if a file is a symlink from C++.

Hard links:

Linux: Again (usually) not possible without searching all files and checking the inode number.

Windows: Depending on the used file system, FindFirstFileName and FindNextFileName
could be useful, but as said, they won´t always work (ReFS...)

Community
  • 1
  • 1
deviantfan
  • 11,268
  • 3
  • 32
  • 49
  • 1
    On Windows, a symlink is implemented by a reparse point, which can be detected by `GetFileAttributes()` and `FindFirstFile()`/`FindNextFile()` via the `FILE_ATTRIBUTE_REPARSE_POINT` attribute. Hard links must also be detectable as well (I'm not sure how) because the command-line `fsutil` tool has a [`hardlink list`](https://technet.microsoft.com/en-us/library/cc788097.aspx) parameter that can find hard links to a given file (at least on Win7/2008+) and SysInternals also has a [`FindLinks`](https://technet.microsoft.com/en-us/sysinternals/hh290814) tool for finding hard links. – Remy Lebeau Jul 01 '15 at 17:33
  • @RemyLebeau I forgot `FILE_ATTRIBUTE_REPARSE_POINT` (facepalm) . But still, it´s not that easy to check if it is a symlink or another kind of RP. And yes, hard links are detectable like described in the answer, but only on certain file systems (NTFS yes, ReFS not in all versions...) – deviantfan Jul 01 '15 at 17:36
  • 1
    Actually, when the `FILE_ATTRIBUTE_REPARSE_POINT` attribute is present on a file/folder, the `WIN32_FIND_DATA` struct from `FindFirstFile()`/`FindNextFile()` specifies the [Reparse Point Tag](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365511.aspx) in the `dwReserved0` field. One of the available tags is `IO_REPARSE_TAG_SYMLINK`. This is [documented](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363940.aspx): "*you can determine if a reparse point is a symbolic link by testing whether the tag value is IO_REPARSE_TAG_SYMLINK*." – Remy Lebeau Jul 01 '15 at 18:18
  • 1
    FindFirstFileNameW/FindNextFileNameW should work on all standard, local file systems that have support for hard links. That's probably all you need so far as hard links go. – Harry Johnston Jul 01 '15 at 21:06