Jennette
Jennette

Reputation: 461

How to format a Windows path to a Unix path on Cygwin command line

When using Cygwin, I frequently copy a Windows path and manually edit all of the slashes to Unix format. For example, if I am using Cygwin and need to change directory I enter:

cd C:\windows\path 

then edit this to

cd C:/windows/path  

(Typically, the path is much longer than that). Is there a way to use sed, or something else to do this automatically? For example, I tried:

echo C:\windows\path|sed 's|\\|g'  

but got the following error

sed: -e expression #1, char 7: unterminated `s' command

My goal is to reduce the typing, so maybe I could write a program which I could call. Ideally I would type:

conversionScript cd C:/windows/path 

and this would be equivalent to typing:

cd C:\windows\path

Upvotes: 16

Views: 18281

Answers (5)

David Dyck
David Dyck

Reputation: 128

to answer your question to achieve

cd C:\windows\path

since you are in bash this just works as you want - but add single quotes

cd 'C:\windows\path'

As noted by @bmargulies and @Jennette - cygpath is your friend - it would be worth it to read the cygwin man page

man cygpath

Upvotes: 0

anders
anders

Reputation: 195

You replace back-slash by slash using unix sed

Below I use star "*" to seperate fields in s directive


     sed "s*\\\*/*g"

The trick is to use one back-slash more than you might think needed

Upvotes: 1

Jennette
Jennette

Reputation: 461

Thanks all. Apparently all I need are single quotes around the path:

cd 'C:\windows\path'

and Cygwin will convert it. Cygpath would work too, but it also needs the single quotes to prevent the shell from eating the backslash characters.

Upvotes: 28

ghostdog74
ghostdog74

Reputation: 342363

cmd.exe doesn't like single quotes. You should use double quotes

C:\test>echo C:\windows\path|sed "s|\\|/|g"
C:/windows/path

Upvotes: 1

bmargulies
bmargulies

Reputation: 100032

Read about the cygpath command.

somecommand `cygpath -u WIN_PATH`

e.g.

Upvotes: 15

Related Questions