Windows – Use mklink in msys

mklinkmsyswindows

I understand that Windows later-than-or-equal-to Vista provides the mklink shell command. I'd like to make use of this from the Msys terminal. Any idea how?

When I enter mklink on the msys terminal, it outputs sh: mklink: command not found. Msys only provides a fake ln utility which appears to be effectively the same as cp.

I tried writing a shell script to open a Windows shell and run mklink within it, but when my shell script tries to execute cmd /C <instructions>, msys brings the Windows shell to the foreground of the current terminal and leaves it there, without running the instructions.

Best Answer

Using cmd //c mklink directly on the MSYS bash command line should work.

$ cmd //c 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.

Note: The mklink command and arguments need to be provided as a single argument to cmd. Quote the entire command like so

cmd //c 'mklink link target'

Note that the command would normally be

cmd  /c 'mklink link target'

which would work in Cygwin and other shell environments, and even in an existing CMD session.  However, msys seems to mangle command-line arguments (to Windows commands), and it interprets /c as a pathname to the root of the C disk, and converts it to c:\.  Typing //c has been found to cause msys to pass the /c option to cmd.  See How to run internal cmd command from the msys shell?

Related Question