1

I want to create a bash file (.sh file) that creates a symlink using the dos mklink command.

mklink /D "path" "path"

Use this window command in bash

But when I tried that I got an error: mklink: command not found

How to solve that?

pehcahn
  • 11
  • 3
  • You don't use a Windows command, you use this instead: `ln -s /path/to/dir /path/to/symlink` – Compo May 20 '20 at 13:34
  • @Compo can you please give me an example – pehcahn May 20 '20 at 13:37
  • What was wrong with the one I provided? – Compo May 20 '20 at 13:41
  • @Compo by giving an example path. – pehcahn May 20 '20 at 13:45
  • @Compo Do I have to use `C:\Program Files\Java` inplace of /path/to/dir or /path/to/ – pehcahn May 20 '20 at 13:57
  • @Compo I am just confused and asking for help. – pehcahn May 20 '20 at 13:58
  • It is not enough to say you're running bash. Is this MSYS2 bash? Cygwin bash? WSL bash? The environment matters as to whether `ln -s` is going to do what you want. IIRC, Cygwin `ln -s` uses its own form of symlinks. MSYS2 `ln -s` creates copies unless you set the environment variable `MSYS=winsymlinks:nativestrict`. WSL `ln -s` automatically creates native Windows symlinks on drvfs volumes such as "/mnt/c". – Eryk Sun May 20 '20 at 14:07
  • I'm not guessing what's in your mind, or teaching you how to use bash syntax for directory paths, especially as you clearly didn't use that location in your question, only `"path"`. Only you know the path and name you're wanting for the symlink. – Compo May 20 '20 at 14:08
  • 1
    If `ln -s` doesn't create a native Windows symlink in your context, you will have to run `cmd.exe /c "mklink "` because `mklink` is an internal command of the CMD shell. BTW, CMD is not DOS; it's a Windows console application. – Eryk Sun May 20 '20 at 14:08

1 Answers1

1

For use mklink in you Windows, read the information on ss64.com about.

Elevation

  • By default, only Administrators can create symbolic links. The security setting 'Create symbolic links' can be granted at: Configuration\Windows Settings\Security Settings\Local Policies\User Rights Assignment\*

  • Creating a symbolic link requires elevation, but from Windows 10 build 14972, symlinks can be created without needing to elevate the console as administrator - this does however require that you have Developer Mode enabled.

So, you can enable Developer Mode enabled

enter image description here

enter image description here

enter image description here

I prefer to apply a boot after any changes made to the system settings, this is my habit, so I restarted and typed:

C:\Users\ecker>mklink /D "%userprofile%\Documents\Call of Duty Black Ops II Saves" "C:\Program Files (x86)\Steam\steamapps\common\Call of Duty Black Ops II\players"
symbolic link created for C:\Users\ecker\Documents\Call of Duty Black Ops II Saves <<===>> C:\Program Files (x86)\Steam\steamapps\common\Call of Duty Black Ops II\players
C:\Users\ecker>mklink
Creates a symbolic link.

MKLINK [[/D] | [/H] | [/J]] Link Target

        /D      Creates a directory symbolic link.  Default is a file
                symbolic link.
        /H      Creates a hard link instead of a symbolic link.
        /J      Creates a Directory Junction.
        Link    Specifies the new symbolic link name.
        Target  Specifies the path (relative or absolute) that the new link
                refers to.

Obs.: You can also activate Developer Mode for Windows 10 using PowerShell, also for cmd command line or batch file:

reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock" /t REG_DWORD /f /v "AllowDevelopmentWithoutDevLicense" /d "1"

Io-oI
  • 2,514
  • 3
  • 22
  • 29
  • I would assume that in order to modify/create a registry key, value and data in `HKEY_LOCAL_MACHINE`, that you would however be required to 'Run as administrator' the `reg.exe` cmd command line or batch file. And if you were already allowed to do that, you'd also be able to create your symlink! – Compo May 20 '20 at 15:35
  • Creating symbolic links does not require elevation, i.e. high integrity level. Microsoft's documentation is fundamentally confused on this point. By default, only the administrators group is granted the symlink privilege. Privileges granted to a group are only added to an access token if the group is enabled, and the UAC filtered token for standard access has the administrators group disabled. But if you grant the privilege to a group that's not subject to UAC filtering, such as the "Authenticated Users" well-known group, then it will not be filtered out of the access token. – Eryk Sun May 20 '20 at 16:20
  • In developer mode, the symlink privilege is not required by the filesystem, and for WinAPI `CreateSymbolicLinkW`, the sole purpose of the flag `SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE` is just to prevent it from failing the call if the privilege can't be enabled. However, other functions such as `CopyFileExW`: `COPY_FILE_COPY_SYMLINK` still aren't aware of this and require the privilege even in developer mode. – Eryk Sun May 20 '20 at 16:28
  • @Compo Thank your comment, I not try add the key, just following the GUI to enable dev mode. @ Eryk Sun What explain: I'm local user/admin in Windows 10, and mklink not work start cmd by admin? Only after enable the dev mode in Window 10 it became work, why? But sure the doc is confused, and thank your comment... – Io-oI May 20 '20 at 16:31
  • `mklink` will work fine for any user in any mode if you grant the symlink privilege directly to the user or a non-admin group such as "Authenticated Users". Otherwise if only the administrators group has the privilege, you have to elevate to get the full administrator token (but it's only the group that matters, not the elevated integrity level). CMD was also updated to call `CreateSymbolicLinkW` with the flag `SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE`, so no privilege is required in developer mode. – Eryk Sun May 20 '20 at 16:45
  • Yet functions such as `CopyFileExW` (copying a symlink) and `CreateDirectoryEx` (copyying a symlinkd) fail even though if they *tried* the file-system control `FSCTL_SET_REPARSE_POINT` it would actually succeed. They're just failing early without trying because the privilege isn't held. – Eryk Sun May 20 '20 at 16:48