How can I convert a Windows dir path (say c:/libs/Qt-static
) to the correct POSIX dir path (/c/libs/Qt-static
) by means of standard msys features? And vice versa?
8 Answers
Cygwin, Git Bash, and MSYS2 have a readymade utility called cygpath.exe
just for doing that.
Output type options: -d, --dos print DOS (short) form of NAMEs (C:\PROGRA~1\) -m, --mixed like --windows, but with regular slashes (C:/WINNT) -M, --mode report on mode of file (binmode or textmode) -u, --unix (default) print Unix form of NAMEs (/cygdrive/c/winnt) -w, --windows print Windows form of NAMEs (C:\WINNT) -t, --type TYPE print TYPE form: 'dos', 'mixed', 'unix', or 'windows'

- 78,603
- 9
- 131
- 207

- 20,270
- 5
- 40
- 73
-
1Have a look at this mail: http://sourceforge.net/mailarchive/message.php?msg_id=8686481 – anishsane Dec 13 '12 at 07:17
-
Whoever has downvoted this answer, please also comment about any mistake, so that I could rectify... :-) – anishsane Apr 24 '14 at 10:08
-
5`cygpath` appears to work on MINGW64 (git bash). Not sure if it's because I have cygwin installed as well or has been added since this question - but I got the expected outcome from `cygpath 'd:\something'` - how could it automatically translate a string from the clipboard or custom environment variable? – hood Sep 05 '16 at 22:57
-
@hood: _"Not sure if it's because I have cygwin installed as well"_ yes, that's the likely explanation... _"how could it automatically translate a string from the clipboard or custom environment variable"_ Not sure what is expected here. – anishsane Sep 07 '16 at 06:49
-
Only small caveat, the utility only converts any path string, does not check to see the path exists. – SGM1 Dec 12 '16 at 17:11
-
@SGM1: I think, that's good/intentional. e.g. you may want to do this: `mkdir C:\temp\new_dir` So, you would do `mkdir /cygdrive/c/temp/new_dir`, using `cygpath` to convert the paths. If `cygpath` had been checking the existence of the target, you would not be able to convert such paths... – anishsane Dec 13 '16 at 04:21
-
sooo anoying when utilities don't support -- from std in `pwd | cygpath -w --` is preferrable over to `cygpath -w $(pwd)`
– JJS Jan 04 '19 at 18:50 -
3`cygpath.exe` comes included when you install git-bash. You don't need to have cygwin or even know what cygwin is. It's in path when you're using git-bash – ZYinMD Sep 16 '20 at 02:05
-
@ZYinMD, But then you need to install git-bash. I don't have to install git if I don't need to. Basically, many softwares are cygwin based (like git-bash) and ship a minimal version of cygwin. You can use whatever you have. I have been using cygwin for a long time without having or even knowing git-bash. cygpath.exe comes installed with cygwin. – anishsane Sep 16 '20 at 05:06
-
Yes @anishsane, I should've have worded my comment differently. What I meant was "btw if you (the reader) have already installed git-bash (many do, in 2020), then you already have cygpath in path, without needing to know what cgwin is. – ZYinMD Sep 17 '20 at 19:26
-
Thank you... :-) On an unrelated note, I have moved from cygwin to WSL2. – anishsane Sep 17 '20 at 20:05
I don't know msys
, but a quick google search showed me that it includes the sed
utility. So, assuming it works similar in msys
than it does on native Linux, here's one way how to do it:
From Windows to POSIX
You'll have to replace all backslashes with slashes, remove the first colon after the drive letter, and add a slash at the beginning:
echo "/$pth" | sed 's/\\/\//g' | sed 's/://'
or, as noted by xaizek,
echo "/$pth" | sed -e 's/\\/\//g' -e 's/://'
From POSIX to Windows
You'll have to add a semi-colon, remove the first slash and replace all slashes with backslashes:
echo "$pth" | sed 's/^\///' | sed 's/\//\\/g' | sed 's/^./\0:/'
or more efficiently,
echo "$pth" | sed -e 's/^\///' -e 's/\//\\/g' -e 's/^./\0:/'
where $pth
is a variable storing the Windows or POSIX path, respectively.

- 37,726
- 7
- 50
- 96
-
@Dukales I can't test this, so does this work? If not, what are the error messages? – Rody Oldenhuis Dec 04 '12 at 11:15
-
1
-
2@Dukales: ...why? AFAIK, without the leading slash it indicates a *relative* path, wheras the leading slash makes it an *absolute* path w.r.t. the filesystem root – Rody Oldenhuis Dec 04 '12 at 11:32
-
-
@Dukales: ah, like so. That is entirely true. I intended `$PATH` to be a user-defined variable; I'll edit. – Rody Oldenhuis Dec 04 '12 at 11:38
-
11You know, there is no need to pipe several invocations of `sed`, just pass multiple `-e command` arguments to single instance (like `sed -e one -e two`). – xaizek Nov 17 '14 at 10:43
-
2when using the bash shipped with git, the drive letter needs to be lowercased, and some folder names may begin with a '$' - try this: `echo "$pth" | sed -e 's#\\#/#g' -e 's#\$#/\\$#g' -e 's#^\([a-zA-Z]\):#/\L\1#` – Francois Nov 27 '14 at 10:34
-
2When converting a strings with multiple paths from Windows to POSIX, a leading / has to be added to every path. The more effiicient command version will become: `echo "$pth" | sed -e 's/\\/\//g' -e 's/\([a-zA-Z]\):/\/\1/g'` – Klamer Schutte Dec 29 '15 at 10:48
-
1@Francois Perhaps case is irrelevant now. My version of bash `GNU bash, version 4.3.42(3)-release (x86_64-pc-msys)` with git `2.6.3.windows.1` accepts `/C` or `/c` as equivalent. – Johann Feb 10 '16 at 22:00
-
2In Git Bash (at least; I don't know how Cygwin or other POSIX compatible shells in Windows treat Windows paths), the path prefix `X:` (where `X` is the driver letter) becomes `/x`, so `sed 's/://'` should be changed to `sed -r 's|^([^:]+):|\/\L\1|'` (at least when used in Git Bash; I also took the liberty to change `/` to `|` as I personally find this much less confusing in `sed` scripts). – HelloGoodbye Jan 28 '20 at 15:06
-
1This assumes C: is mounted on /c etc. which is not necessarily the case (true for git-bash, false for cygwin) – Olivier Gérardin Jul 19 '22 at 13:29
Just use cygpath
:
$ cygpath -w "/c/foo/bar"
-> C:\foo\bar
$ cygpath -u "C:\foo\bar"
-> /c/foo/bar
You may wonder: "Do I have cygpath
installed?" Well,
- If you're in git-bash, then yes.
- If you're in cygwin or MSYS2, then yes.
- If you're in another shell, but you have installed git-bash before, then
cygpath
can be found atgit-bash-install-folder\usr\bin\cygpath.exe
. - Else: maybe not, but I'm sure you can find a way to installed it.

- 3,602
- 1
- 23
- 32
The "correct" way in MSYS is:
$ MSYS_NO_PATHCONV=1 taskkill /F /T /IM ssh-agent.exe
This avoids having to manually translate slashes. It simply de-activates the path conversion.

- 3,938
- 2
- 37
- 45
Here is my implementation (tested on git bash).
From POSIX to Windows
sed '
\,/$, !s,$,/,
\,^/, s,/,:/,2
s,^/,,
s,/,\\,g
' <<< "$@"
Works for:
/c/git
relative/dir
c:/git
~
.
..
/c
/c/
./relative/dir
/sd0/some/dir/
except
/
<path with space>
Explanation:
\,^/, s,/,:/,2
(converts /drive/dir/
to /drive:/dir/
) is the heart of it and inserts :
before the 2
nd /
. I use ,
for delim instead of /
for readability. If starting with /
(\,^/,
), then replace /
with :/
for the 2
nd occurrence. I do not want to assume drive letter length of 1 so this works for /sd0/some/dir
.
s,^/,,
removes the leading /
and s,/,\\,g
converts all /
to \
.
\,/$, !s,$,/,
is to handle the corner case of /c
and ensure 2nd /
(/c/
) for the next command to work.
Note:
If here string <<<
does not work in your shell then you can echo
and pipe as
echo "$@" | sed ...
Errata
Here e script

- 4,649
- 6
- 51
- 65
just FYI - at least for my git version 2.26.2.windows.1 e.g. if I have a path like C:\dev\work_setup\msk, I can go directly to Git Bash and type
cd "C:\dev\work_setup\msk"
this will result in current folder being changed to /c/dev/work_setup/msk - so this type of conversion seems to be done automatically, as long as I put the Windows path inside double quotes. Unfortunately I don't have references to original documentation that would back that up.

- 1,442
- 1
- 25
- 39
-
This worked for me whereas "cygpath --unix" did not, it returned the path minus any slashes. – Pellet May 10 '23 at 21:53
My solution works with a list of folders/files and it's done in 2 steps. Suppose you would like to replace a path from D:\example to /example for a list of file where this Windows path has been repetead.
The first step it changes the backlashes into slashes
grep -lr "D:\\\\example" /parent-folder | xargs -d'\n' sed -i 's+\\+\/+g'
Note that parent-folder could be root (/) or whatever you like and -d'\n' parameter is necessary if you have filenames or folder names with white spaces.
Second step it substitutes the D:/example into /example:
grep -lr "D:/example" /parent-folder | xargs -d'\n' sed -i 's+D:+/example+g'
I wanted to share this solution since it tooks me some time to make this 2 lines but it has been really helpfull job (I'm migrating a Windows App to a Linux Server with tons of Windows paths inside').

- 525
- 7
- 16
The answer of @hello_earth is misleading, due to Windows path must be double backslashed like:
cd "e:\\dir\\subdir\\path"
otherwise the shell will find escape-sequences.

- 106
- 1
- 4