How to create a directory hard link in Windows?

14

2

I was trying to create a directory hard link (not a symbolic one).

I've tried this: mklink /d /h newfolder currentfolder but it's telling me Access is denied. I don't understand how is access denied because I'm running batch as administrator.

How do we create a directory hard link?

==
Windows Vista Home Premium SP2

Pacerier

Posted 2011-10-05T03:21:19.390

Reputation: 22 232

Answers

16

I think that hard links are for files only and not directories.

kobaltz

Posted 2011-10-05T03:21:19.390

Reputation: 14 361

1well... under linux-systems you can use mount --bind for folders but i'm not sure if there is something similar in windows! – DJCrashdummy – 2015-10-19T13:37:16.053

4Yes, there is no such thing as a directory hard link, only junction points and symbolic links. – Harry Johnston – 2011-10-05T03:46:32.297

1Hardlinks for directories are technically possible, but need great care to avoid loops in the filesystem. The only OS which allows them is Mac OS X 10.5, for use in Time Machine. – user1686 – 2011-10-05T06:04:19.817

ockquote>

I think that hard links are for files only and not directories. Thanks for the confirmation; I keep intending to read up on symlinks and such: 1 2 3 4

– Synetech – 2011-12-23T03:22:27.027

6

There is no such thing as a hard link to a diretory in Windows. In Windows, you either create a symbolic link to a directory by using the command mklink /d link_name target_dir or you create a junction with mklink /J link_name target_dir.

Differently of hard links, junctions may span multiple volumes and are sometimes called "soft links" by Microsoft, as you can read here:

A junction (also called a soft link) differs from a hard link in that the storage objects it references are separate directories, and a junction can link directories located on different local volumes on the same computer.

Some caveat is required here since Microsoft's nomenclature is not really neat but, in a few words, these are your options to create references to files and directories in Windows:
(1) shortcuts: files whose content is the location of another file. It works more or less like a soft link, with a crucial difference though: it is NOT a directory entry, the link information is stored inside the file. For this reason, it doesn't work with many applications (at least, it works as it is supposed to within the Windows Explorer...);
(2) hard links: created with the command mklink /h. Valid for files only and works within a given volume (i.e., just like in Linux, you cannot hard-link a file in another partition nor in a network drive);
(3) junctions: this beast is really weird. It works with directories only, and - funny thing - can point to directories in other file systems;
(4) symbolic links: it is much like in Linux, and works with directories and files, too. (But tends to require Administrator privileges, which can make it rather inconvenient.) As I mentioned above, it is created with the command mklink /d link_name target_dir for directories (and mklink link_name target_file for files). You can read more about this here.

Humberto Fioravante Ferro

Posted 2011-10-05T03:21:19.390

Reputation: 71

I strongly suggest removing the commentary from your answer – Ramhound – 2017-08-14T02:25:52.940

Is it an opinion, or did I infringe some rule of the forum? – Humberto Fioravante Ferro – 2017-08-14T16:57:26.850

We are not a forum – Ramhound – 2017-08-14T20:24:03.090

Right, comment taken out. My apologies! – Humberto Fioravante Ferro – 2017-08-14T22:33:57.573

Humberto, I kinda like your answer, but there are two problems: 1. The "Otherwise, junctions operate identically to hard links" part in the MSDN quote is dangerously wrong: a profound difference is that a hard link behaves identically to its "target" (or "sibling", actually...) file in that no matter which one you delete, the other will stay unaffected. Junctions (like other soft links) lack this important symmetry. 2. The reference to Joseph's answer makes no sense. +1: "things you can create to point to file system entities (files and directories)" should be "... (files or directories)". – Sz. – 2017-12-17T00:05:50.613

1@Sz you were totally right, and I modified my answer according to your comments (indeed, the answer was a bit fuzzy). Thanks! – Humberto Fioravante Ferro – 2017-12-25T23:03:13.897

Thanks for the follow-up! I failed to point out in my prev. comment that distancing junctions from soft (or sym-) links and trying to relate them to hard links instead ("It is like a hard link, ...") is still a bit off. Junctions really are soft links, in fact they are very much unlike hard links in almost every respect. (Hard links are low-level filesystem objects quite transparent to fs. operations, and e.g. can't "break", once created, unlike other link types that are called "soft" exactly for this reason.) (I'll edit the a. to show what I mean, feel free to reject/adjust. Cheers!) – Sz. – 2018-01-01T14:42:38.850