Cygwin Path and Windows Path Conversion
The filename path issue between unix and Windows can be complex.
Read: http://cygwin.com/cygwin-ug-net/using.html#using-pathnames .
In general, when you are in Cygwin, always use unix-style path, for example, /something/something
. When you need C:/Users/joe/notes.txt
, use /cygdrive/c/Users/joe/notes.txt
.
For example, when i started to use Cygwin, had a problem that caused me few hours. I have a python program that makes a system call of imagemagick's convert
command. This all works fine in OS X. When trying to run it on Windows with Cygwin, i simply changed the input paths from
/Users/xah/web/
to C:/Users/xah/web/
. This does not work. After some hours of looking, here's some detail:
When in Cygwin terminal, the following does not work:
# cygwin complains cannot find the second file # convert: unable to open image `/Users/xah/x2.jpg': No such file or directory. convert C:/Users/xah/x.png C:/Users/xah/x2.jpg convert "C:/Users/xah/x.png" "C:/Users/xah/x2.jpg" # complains cannot find the first file convert /Users/xah/x.png ~/x2.jpg convert "C:/Users/xah/x.png" /Users/xah/x2.jpg convert /Users/xah/x.png /Users/xah/x2.jpg convert /Users/xah/x.png x2.jpg
The following creates the image, but the result image is bad.
# creates image, but bad img data convert C:/Users/xah/x.png "C:\Users\xah\x2.jpg" convert C:/Users/xah/x.png C:\\Users\\xah\\x2.jpg # this one creates image in the in current dir as Usersxahx2.jpg convert C:/Users/xah/x.png C:\Users\xah\x2.jpg
The following works:
convert "C:/Users/xah/x.png" ~/x2.jpg
convert C:/Users/xah/x.png ~/x2.jpg
convert C:/Users/xah/x.png /cygdrive/c/Users/xah/x2.jpg
convert /cygdrive/c/Users/xah/x.png /cygdrive/c/Users/xah/x2.jpg
Here's some lesson learned:
- When you are in Cygwin terminal, you are running bash. That means, paths like “~” are interpreted by bash first. It also means, backslash means quoting the next char, and double straight quotes quotes your string, just as bash in unix.
- Cygwin provides a map from Windows path to unix path, by “mount”. For example,
C:/something
gets mapped to/cygdrive/c/something
. - Not sure what mechanism
/cygdrive/c
is mapped to the disk. Similarly, how/usr/bin
actually refers to the real location repesented by the Windows syntaxC:/cygwin/usr/bin
. Probably some Cygwin internal. - When in Cygwin, stick with unix style path. When you need
C:/something
, use/cygdrive/c/something
. - Unrelated to Cygwin, but Windows's API actually understand forward slash for path separator, as well as path without a drive letter. It's just that some apps such as cmd.exe doesn't deal with it. Windows PowerShell does. (see Wikipedia's “path” article on this.)