Use msysgit/"Git for Windows" to navigate Windows shortcuts?
I use msysgit on Windows to use git, but I often want to navigate through a Windows-style *.lnk
shortcut. I typically manage my file structure through Windows' explorer, so using a different type of shortcut (such as creating hard or soft link in git) isn't feasible. How would I navigate through this type of shortcut?
For example:
PCUser@PCName ~
$ cd Desktop
PCUser@PCName ~/Desktop
$ ls
Scripts.lnk
PCUser@PCName ~/Desktop
$ cd Scripts.lnk
sh.exe": cd: Scripts.lnk: Not a directory
Is it possible to change this behavior, so that instead of getting an error, it just goes to the location of the directory? Alternatively, is there a command to get the path in a *.lnk
file?
EDIT: I've heard that the inverse of this exists for cygwin, allowing you to create a symlink which works with explorer.
*ahem*
First, compile the following AutoHotkey script:
FileGetShortcut, %1%, shortcut_target
FileAppend, %shortcut_target%, *
ExitApp
Place the .EXE
file in a %PATH% directory. I named mine follow.exe
.
Now, you can effectively follow a Windows .LNK
file in your working directory by using the following syntax:
cd `follow Shortcut.lnk`
where Shortcut.lnk
's target is a valid directory.
Demonstration:
Once you've set up your follow.exe
, you can add the following shell function to your ~/.bashrc
file to simplify the syntax even further. Thanks, Daniel!
function cd
{
if [[ ".lnk" = "${1:(-4)}" && -f "$1" ]] ;
then builtin cd "$( follow "$1" )" ;
else builtin cd "$1" ;
fi
}
Now, you can follow .LNK
files with just cd
!
cd Shortcut.lnk
Demonstration:
It seems that it's not possible at this moment, and the recommended approach is to use the Junction utility:
About creating symbolic on msys
Update:
Thank you iglvzx for your answer.
However, in my case sometimes the bash autocompletion puts an /
after the shortcut, like cd /f/downloads/scripts.lnk/
, so I use it as an excuse to play with bash scripting and adjust your script, also checking for not acceptable shortcuts (broken or pointing to files):
cd() {
shopt -s nocasematch
if [[ -z "$1" ]];
then
echo Error: missing operand;
return 1;
fi;
if [[ "$1" == "/" ]];
then
destination=$1;
else
destination=${1%/};
fi;
extension=${destination##*.}
if [[ -f "$destination" && $extension == "lnk" ]];
then
finaldestination=`follow "$destination"`;
if [[ -z "$finaldestination" ]];
then
echo Error: invalid destination;
return 2;
fi;
if [[ -f "$finaldestination" ]];
then
echo Error: destination is a file;
return 3;
fi;
builtin cd "$finaldestination";
else
builtin cd "$destination";
fi;
}
Opening shortcuts from within msys
is possible with cmd
via the /C
flag:
cmd //C "C:\Shortcut.lnk"
If it's a shortcut to a folder, it will be opened. The slash has to be escaped so msys doesn't convert /C
to C:\
.
Relative paths work as well, and quotes aren't needed for simple paths. If the lnk
file is in current folder:
cmd //C Shortcut.lnk