3

I have an MSYS installation, and I am writing a bash script to set up some files. I would like to make a directory symbolic link from the bash script in MSYS, but to do that I will need to use mklink /D, which is a windows command. ln does not work with NTFS symbolic links, it only seems to copy the folder, so I cannot use that unfortunately.

I have a directory mounted as /opt in MSYS using fstab. The real directory is C:\opt (but it could be anything)

mklink expects a Windows-style path like C:\opt. However, I can only provide /opt which it cannot work with. Is there any way to get the "real" path of /opt?

Alternatively, if there's a way to get ln to work like mklink /D that'll be great. But I can't seem to find a way (there does exist a way in Cygwin however but it didn't seem to work for me)

Note: I do not have cygwin, and I do not want to install external software (including cygwin)

9a3eedi
  • 696
  • 2
  • 7
  • 18

2 Answers2

1

I figured it out somehow, although it is kinda an ugly hack

If I want to figure out the real Windows path of the current directory, I can used pwd -W, which is apparently an MSYS-only feature

In my script, I can probably do something like:

realpath=`cd /opt && pwd -W`

to get the real path of /opt. I tested it and it seems to work.

I'd appreciate a less ugly method though

9a3eedi
  • 696
  • 2
  • 7
  • 18
  • 1
    I think you don't need the `cd -` at the end because the whole thing runs in a subshell anyway. So that makes it very slightly less ugly. :P – ben Jan 26 '16 at 11:43
  • That prints `C:/blah/opt`. Any way to get it to output backslashes (`C:\blah\opt`)? – rustyx Nov 03 '21 at 11:37
  • @rustyx You can pipe to sed. So something like `realpath=\`cd /opt && pwd -W | sed s/\\//\`` I could not test this anymore though as I stopped using MinGW – 9a3eedi Nov 04 '21 at 07:34
1

I call this "winpath" and stick it in my /usr/bin folder for msys. Only pwd seems to support revealing the underlying rooted path:

#!/bin/bash

if [[ -f "$1" ]]; then
    dir=$(dirname "$1")
    add=/$(basename "$1")
else
    dir="$1"
    add=""
fi

pushd $dir > /dev/null
echo $(pwd -W)$add
popd > /dev/null
Erik Aronesty
  • 11,620
  • 5
  • 64
  • 44