What is the windows equivalent to the ln -s <target folder> <link folder> unix symbolic link command?

11,399

Solution 1

Try this :

mklink /D .\ venv\lib\python2.7\site-packages\httplib2

Note : mklink [OPTION] LINK TARGET (link and target are flipped compared to linux's ln -s)

Mklink Command Syntax :

MKLINK has 3 options /D, /H and /J. You also need to specify the path to the new symbolic link and the path to the original file or directory.

/D – used to create symbolic links for directories (d for directory)

/H – used to create hard links (h for hard link)

/J – used to create directory junction (j for junction)

By the way, always prefer mklink /D over mklink /J. Windows explorer will delete the entire contents of a junction (the latter) whereas when deleting a directory link (the former) it will just remove the link.

The dot . is the current directory (from where you are running the command). In the example above, I changed it to .\ to make it explicit.

For files : Helpful link.

If you can't get privilege with /D, use a hard link (option /H) :

mklink /H .\six.py venv\lib\python2.7\site-packages\six.py

Solution 2

'.' stands for the current folder, in both *nix-land and Windows. So those commands are making symbolic links right then and there.
These would be the same as saying:

ln -s venv/lib/python2.7/site-packages/twilio twilio

Or in Windows (Vista, 7, 2008 and up):

mklink /d twilio venv\lib\python2.7\site-packages\twilio 

where

  • twilio is the target or link to create
  • venv\lib\python2.7\site-packages\twilio is the source directory

Remember that mklink has the opposite soure | target syntax that ln -s has.

ln is source -> target
mklink is target -> source

Share:
11,399
jmatthews
Author by

jmatthews

Updated on September 15, 2022

Comments

  • jmatthews
    jmatthews over 1 year

    I'm attempting to follow the following tutorial for creating a program environment for the Twilio and App Engine library.

    https://developers.google.com/appengine/articles/twilio

    I'm good up until the point it says:

    Link the Twilio library and its dependencies into your project:

    $ ln -s venv/lib/python2.7/site-packages/twilio .
    
    $ ln -s venv/lib/python2.7/site-packages/httplib2 .
    
    $ ln -s venv/lib/python2.7/site-packages/six.py .
    

    I've researched and it appears I'll be using something along the lines of

    mklink /d venv\lib\python2.7\site-packages\twilio .
    

    if I've understood correctly I basically need to understand what the "." stands for, as that is the target of the symbolic link? (Not certain about that.)

    I'm using cmd.exe for the shell and could really use the help of someone that understands Unix better than I.

    EDIT:

    After reviewing my directory the path after venv is venv\lib\site-packages. There are already folders for Twilio and httplib2 at that point of the directory. Six exists at that point in a file named six.py.

    Is the intent of the Unix command that I create a symbolic link from those existing folders to the working directory? Because what its doing is telling me I can't create existing files for both Twilio and httplib2. (There are already folders in the venv\lib\site-packages directory, and it WILL let me do a symlink for six, but then it recursively creates 4500 more layers deep of the entire six folder.)

  • jmatthews
    jmatthews almost 11 years
    Alright, digging a little deeper I think I have more issues than I considered. venv\lib\python2.7\site-packages\ doesnt exist. venv\lib\site-packages\ does. I hate how most python stuff assumes Unix. Now I'm unsure how to proceed. It also appears to me that the order of arguments are flipped in the mklink versus the ln command. I've tried mklink /D . venv\Lib\site-packages\six and it tells me Cannot create a file that already exists.
  • jmatthews
    jmatthews almost 11 years
    Alright, I'm making progress. The arguments are flipped from ln in Unix so the correct command is mklink /D twilio venv\lib\site-packages\twilio which works great, however, the first two items to link are directories. The final one is a single .py file and won't link like the first two will. Is there any magic sauce to doing a symlink on a file versus a directory?
  • jmatthews
    jmatthews almost 11 years
    Alright, I have this nailed down and wanted to share the exact answer in case someone tackles this in the future. Compared to the Unix ln the argument order is flipped on mklink. The ported version of the tutorial is: mklink /D twilio venv\lib\site-packages\twilio mklink /D httplib2 venv\lib\site-packages\httplib2 mklink six.py venv\lib\site-packages\six.py note on the last link, which is to a file, not a directory, you omit the /D as file linkage is the default.
  • jmatthews
    jmatthews almost 11 years
    Hey Golgauth, thanks for the help. For anyone else running into this issue, the directory structure is slightly different on Windows, so pay close attention to that part, also the arguments are flipped from ln to mklink, in mklink it is target, source, not source, target. Finally the /J junction flag no longer deletes the entire directory, that was only true of Vista when they first added the functionality. I posted a quick blog with the corrected code and description here. anarchicorder.blogspot.com/2013/06/…