Home Page
  • December 04, 2024, 04:30:55 pm *
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

News:

Official site launch very soon, hurrah!


Author Topic: Windows “ln” (symbolic linking) support for Cygwin  (Read 16185 times)

Dakusan

  • Programmer Person
  • Administrator
  • Hero Member
  • *****
  • Posts: 550
    • View Profile
    • Dakusan's Domain
Windows “ln” (symbolic linking) support for Cygwin
« on: July 25, 2013, 06:05:08 am »

Original post for Windows “ln” (symbolic linking) support for Cygwin can be found at https://www.castledragmire.com/Posts/Windows_“ln”_(symbolic_linking)_support_for_Cygwin.
Originally posted on: 07/25/13

The “ln -s” command in Cygwin creates a fake symbolic link only supported in Cygwin. I whipped up the following script to create a true windows symbolic link that is supported in both Windows and Cygwin (it shows up as a symlink in Cygwin).

TARGET=`echo $1 | perl -pe 's/\\//\\\\/g'`; #Determine the target from the first parameter (where we are linking to). Change forward slashes to back slashes
LINK=`echo $2 | perl -pe 's/\\//\\\\/g'` #Determine the link name from the second parameter (where the symlink is made). Change forward slashes to back slashes
cmd /c mklink $3 $4 $5 $6 "$LINK" "$TARGET" #Perform the windows mklink command with optional extra parameters
Note that for the link name, you have to include the full filename, not just specify a directory.
See here for information on mklink and its switches. Specifically:
  • /d : directory symlink (more hard)
  • /j : directory junction (more soft)
This handles the problem a little more directly than my other post on the topic ("Symlinks in a Windows programming environment").

[Edit on 2016-01-12 @ 12:34am]
And once again, I have a new version of the code. This version has the following advantages:
  • No longer limited to just 4 extra parameters (dynamic instead of static)
  • Can now just specify your directory as the link location, and the filename will be automatically filled in
  • Handles spaces better
Do note, if you want to link directly to a hard drive letter, you must use "c:/" instead of "/cygdrive/c/"

#Get the target and link
TARGET="$1"
shift
LINK="$1"
shift

#If the link is already a directory, append the filename to the end of it
if [ -d "$LINK" ]; then
   #Get the file/directory name without the rest of the path
   ELEMENT_NAME=`echo "$TARGET" | perl -pe 's/^.*?\/([^\/]+)\/?$/$1/'`

   #Append the file name to the target, making sure there is only 1 separating "/"
   LINK=`echo "$LINK" | perl -pe 's/^(.*?)\/?$/$1/'`
   LINK="$LINK"/"$ELEMENT_NAME"
fi

#Replace forward slashes with back slashes
TARGET=`echo $TARGET | perl -pe 's/\\//\\\\/g'`
LINK=`echo $LINK | perl -pe 's/\\//\\\\/g'`

#Perform the windows mklink command with optional extra parameters
cmd /c mklink "$@" "$LINK" "$TARGET"
Logged

Dakusan

  • Programmer Person
  • Administrator
  • Hero Member
  • *****
  • Posts: 550
    • View Profile
    • Dakusan's Domain
Re: Windows “ln” (symbolic linking) support for Cygwin
« Reply #1 on: November 08, 2015, 08:33:10 pm »

Note: This script need to be rewritten using cygpath
[Edit on 2015-01-12 @ 1:01am]Nevermind on this. Using cygpath forces absolute paths, which is not always desirable. Hmm... maybe I should add a switch that would do that behavior.
« Last Edit: January 12, 2016, 02:02:06 am by Dakusan »
Logged

Dakusan

  • Programmer Person
  • Administrator
  • Hero Member
  • *****
  • Posts: 550
    • View Profile
    • Dakusan's Domain
Re: Windows “ln” (symbolic linking) support for Cygwin
« Reply #2 on: April 26, 2016, 03:45:08 am »

Just found out that adding "winsymlinks:nativestrict" to your CYGWIN variable in bash makes all internal symlinks use actual windows NTFS symlinks. This is a MUCH better solution, as it will then support all commands that work with symlinks, including rsync.
Logged