在WSL中切换Windows/Linux路径表达方式

admin 2025年1月11日14:35:51评论13 views字数 1035阅读3分27秒阅读模式

http://scz.617.cn:8/windows/202206120354.txt

Q:

When we use WSL shell on windows, it is annoying to convert the path back and forth, is it possible to make our life easier?

A: bluerust 2022-06-12

WSL provides a tool named wslpath, which can help convert the paths between Windows and Linux, the usage is very simple:

wslpath '<path>'

The quotes are necessary to avoid escaping the path when it is Windows format.

By virtue of this tool, we can add to following code to .bashrc:

function _pushd ()
{
    [[ "$1" = *"\"* ]] && builtin pushd $(wslpath -a "$1") || builtin pushd "$1";
}

function _cd ()
{
    [[ "$1" = *"\"* ]] && builtin cd $(wslpath -a "$1") || builtin cd "$1";
}
#
# pushd 'x:temp'
# cd 'x:temp'
#
# the path must be quoted with either double quotes or single quote
#
alias pushd='_pushd'
alias cd='_cd'

After updating the rc file, we need to source it:

source ~/.bashrc

Now, you can use the Windows path with cd and pushd commands:

pushd 'x:temp'
cd 'x:temp'

Please keep in mind that, if you omit the slash following the colon sign,wslpath would treat it as a directory name, for instance:

$ wslpath -a x:
/mnt/c/x:

原文始发于微信公众号(青衣十三楼飞花堂):在WSL中切换Windows/Linux路径表达方式

免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2025年1月11日14:35:51
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   在WSL中切换Windows/Linux路径表达方式https://cn-sec.com/archives/1125728.html
                  免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉.

发表评论

匿名网友 填写信息