Last active
November 21, 2022 10:00
-
-
Save geedew/4ea58b7711256b417c2d62a7f325bf20 to your computer and use it in GitHub Desktop.
A cross platform symlink method in Bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pulled from https://stackoverflow.com/questions/18641864/git-bash-shell-fails-to-create-symbolic-links | |
# https://stackoverflow.com/users/124119/camilo-martin | |
# Detect windows (assumes we are in 'msysgit' or similar). | |
windows() { [[ -n "$WINDIR" ]]; } | |
# Cross-platform symlink function. | |
# With one parameter, it will check whether the parameter is a symlink. | |
# With two parameters, it will create a symlink to a file or directory, | |
# with syntax: link $linkname $target | |
link() { | |
if [[ -z "$2" ]]; then | |
# Link-checking mode. | |
if windows; then | |
fsutil reparsepoint query "$1" > /dev/null | |
else | |
[[ -h "$1" ]] | |
fi | |
else | |
# Link-creation mode. | |
if windows; then | |
# Windows needs to be told if it's a directory or not. Infer that. | |
# Also: note that we convert `/` to `\`. In this case it's necessary. | |
if [[ -d "$2" ]]; then | |
cmd <<< "mklink /D \"$1\" \"${2//\//\\}\"" > /dev/null | |
else | |
cmd <<< "mklink \"$1\" \"${2//\//\\}\"" > /dev/null | |
fi | |
else | |
# You know what? I think ln's parameters are backwards. | |
ln -s "$2" "$1" | |
fi | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm confused (just started using windows after 15years without it), does mklink works in reverse?
For
ln
it's $1 = source, $2 = targetEDIT: It is... how confusing ahaha, so I personally will reverse them as I'm more used to ln's way
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/mklink
Thanks