如何在 Cygwin 命令行上将 Windows 路径格式化为 Unix 路径
使用 Cygwin 时,我经常复制 Windows 路径并手动将所有斜杠编辑为 Unix 格式。例如,如果我使用 Cygwin 并且需要更改我输入的目录:
cd C:\windows\path
然后将其编辑为
cd C:/windows/path
(通常,路径比该路径长得多)。有没有办法使用 sed 或其他方法自动执行此操作?例如,我尝试过:
echo C:\windows\path|sed 's|\\|g'
但出现以下错误
sed: -e expression #1, char 7: unterminated `s' command
我的目标是减少打字,所以也许我可以编写一个可以调用的程序。理想情况下,我会输入:
conversionScript cd C:/windows/path
这相当于输入:
cd C:\windows\path
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
谢谢大家。显然我需要的只是路径周围的单引号:
cd 'C:\windows\path'
并且 Cygwin 将转换它。 Cygpath 也可以工作,但它还需要单引号来防止 shell 吃掉反斜杠字符。
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.
了解 cygpath 命令。
例如
Read about the cygpath command.
e.g.
cmd.exe
不喜欢单引号。你应该使用双引号cmd.exe
doesn't like single quotes. You should use double quotes您可以使用 unix sed 将反斜杠替换为斜杠
下面我使用星号“*”来分隔 s 指令中的字段
技巧是使用比您认为需要的多一个反斜杠
You replace back-slash by slash using unix sed
Below I use star "*" to seperate fields in s directive
The trick is to use one back-slash more than you might think needed
回答你的问题以实现
因为你在 bash 中,所以它可以按你想要的方式工作 - 但添加单引号
cd 'C:\windows\path'
正如 @bmargulies 和 @Jennette 所指出的 - cygpath 是你的朋友 - 阅读 cygwin 手册页
人cygpath
to answer your question to achieve
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