【问题标题】:How to open the current directory on Bash on Windows?如何在 Windows 上打开 Bash 上的当前目录?
【发布时间】:2017-04-14 06:16:53
【问题描述】:

我知道在Mac OS10中,人们使用open .命令打开当前目录。

请问有人知道在 Windows 上的 Bash 下执行相同任务的适当命令吗?

干杯!

【问题讨论】:

  • 你的问题是什么意思?想象一下,您正在查找一些 Word 文档。然后你想打开一个Ubuntu应用程序,立即进入Word文档的目录,MS-Word应用程序,...?

标签: windows bash ubuntu


【解决方案1】:

最简单的方法是编辑.bash_aliases

nano ~/.bash_aliases
# add
alias open='explorer.exe $1'
# save and close: cltr-s, cltr-x
source ~/.bash_aliases

现在,我们可以使用:open .open documents

【讨论】:

    【解决方案2】:

    要通过 windows 子系统 for linux (WSL) 打开当前目录,请使用:

     $ explorer.exe .
    

    【讨论】:

      【解决方案3】:

      试试这个

      $ explorer .
      

      这将适用于 Windows 中的 bash 命令。我正在使用 R studio,并且能够打开目录。

      【讨论】:

        【解决方案4】:

        使用 Git for Windows 附带的 MINGW64 shell,我在我的 .profile 中创建了这些函数:

        function towinpath {
            { cd "$1" && pwd -W; } | sed 's|/|\\|g'
        }
        
        function open {
            path_to_open=""
        
            if [ -f "$1" ]; then
                filename=$(basename "$1")
                win_dirname=$(towinpath "$(dirname "$1")")
                path_to_open="$win_dirname\\$filename"
            elif [ -d "$1" ]; then
                path_to_open=$(towinpath "$1")
            else
                # Take our chances with windows explorer ...
                path_to_open="$1"
            fi
        
            if [ -z "$path_to_open" ]; then
                echo "Failed to open $1"
            else
                explorer "$path_to_open"
            fi
        }
        

        这似乎适用于您可以在 Windows 文件资源管理器中输入的所有内容:

        open .                         # Opens the current directory
        open ../                       # Opens the directory above this one
        open /c/Windows                # Opens C:\Windows
        open ~/AppData                 # Opens my AppData folder
        open ~/*.csv                   # Opens the first CSV file in my home directory in Excel
        open https://stackoverflow.com # Opens StackOverflow in my browser!
        
        # Opens paths with spaces in them too:
        open /c/Program\ Files/Sublime\ Text\ 3/sublime_text.exe
        open '/c/Program Files/Sublime Text 3/sublime_text.exe'
        

        【讨论】:

          【解决方案5】:

          在 WSL2 中,open 命令现在正在执行 start 在 Windows 命令中所做的工作。我只是在我的 .bashrc 中放了一个别名

          start="open"
          

          现在您可以根据自己的喜好选择start .open .

          【讨论】:

            【解决方案6】:

            您可以使用以下命令: explorer .

            【讨论】:

              【解决方案7】:

              这个命令应该可以做到:

              $ explorer
              

              【讨论】:

                【解决方案8】:

                开始。 - 这相当于 open 。在 bash 中

                【讨论】:

                  【解决方案9】:

                  我正在使用这个功能:

                  open()
                  {
                      explorer.exe  `wslpath -w "$1"`
                  }
                  

                  因此,如果您在 /mnt/c/Users/ 并想打开该文件夹,只需输入 open .

                  wslpathwill resolve only paths from the Windows system,请注意。如果你想做类似open ~ 的事情,它不会起作用,你会得到:

                  wslpath: /home/my-user: Result not representable

                  命令用法

                  wslpath usage:
                    -a    force result to absolute path format
                    -u    translate from a Windows path to a WSL path (default)
                    -w    translate from a WSL path to a Windows path
                    -m    translate from a WSL path to a Windows path, with ‘/’ instead of ‘\\’
                  
                    EX: wslpath ‘c:\users’
                  

                  证明这是有效的:

                  【讨论】:

                  • 抱歉没有回答问题
                  • 对于 Windows 上的 Ubuntu Bash,我得到 wslpath: .: Invalid argument
                  • 你必须先设置别名@dotnetCarpenter
                  • 我正在投票,因为它有效,但解释很糟糕。首先open 不是别名而是函数。当您打开终端时,您可以输入.profile.bashrcbash_aliasessource。其次,wslpath 现在可以将lxfstmpfs 中的路径正确转换为Windows 路径。即Linux 子系统中的路径。
                  • @dotnetCarpenter 如果我的答案太“糟糕”,请随时修改我的答案
                  【解决方案10】:

                  如果start . 不适合你,它本质上与运行explorer.exe . 相同,所以你可以为它创建一个别名,这就是我所做的。

                  alias start="explorer.exe"
                  

                  旁注:另一个有用的是BROWSER。 explorer.exe 能够启动您的默认网络浏览器。当您运行打开 Web 浏览器的脚本(例如启动 React.js 开发服务器)时,这会派上用场。

                  export BROWSER="explorer.exe"
                  

                  【讨论】:

                    【解决方案11】:

                    我已经添加了

                    alias open='explorer.exe  `wslpath -w "$1"`'
                    

                    .bashrc 文件

                    输入open时打开当前文件夹

                    我正在使用 windows ubuntu 子系统。

                    【讨论】:

                      【解决方案12】:

                      要在所有类型的路径(Windows 样式和 Linux 样式)下工作,请执行以下操作 (the answer of mine to my own question on SU):

                      (我的挑战是如何在当前工作目录中使用 Linux 风格的路径打开资源管理器以进行查看,如果您要进行修改或执行除查看之外的其他操作,风险自负,也请阅读Do not change Linux files using Windows apps and tools):

                      explorer.exe "C:\Users\userNmae\AppData\Local\Lxss$(sed 's:/:\\:g' <<<"$PWD")"
                      

                      这将在您的工作目录中打开资源管理器。你现在唯一需要的就是定义一个函数来让它工作。您可以将其添加到您的 .bashrc 并获取它或重新打开 Bash。

                      xplor(){ 
                          explorer.exe "C:\Users\userName\AppData\Local\Lxss$(sed 's:/:\\:g' <<<"$PWD")";
                       }
                      

                      注意:在此处将userName 替换为您的Windows 用户帐户名。

                      【讨论】:

                        【解决方案13】:

                        只需输入

                        start .
                        

                        它将打开当前目录文件夹。

                        【讨论】:

                        • /mnt/c/Windows/System32/start: line 1: $'AcrSch2Svc\r': command not found 这种情况我该怎么办?
                        • 在带有 Git Bash 的 Windows 上为我工作。
                        • 谢谢你,先生,我每隔几个月就会寻找这个命令,并且总是以包含一些路径配置和东西的解决方案结束,所以你今天让我很开心
                        【解决方案14】:

                        您现在可以从 bash 子系统调用 explorer.exe。我已经设置了一个别名来使用它。我已将副本添加到剪贴板别名中,以便更好地衡量。

                        别名:

                        alias open="explorer.exe"
                        alias pbcopy="clip.exe"
                        

                        示例:

                        cat ~/.ssh/id_rsa.pub | pbcopy
                        open .
                        open "D:\\Dir"
                        

                        open 别名与. 配合得很好,但如果要指定目录,则需要将 Windows 路径传递给它。

                        【讨论】:

                        • 关于windows路径限制,可以使用cygpath --windows &lt;path&gt;命令进行转换。 Read more here.
                        • wslpath 是子系统版本
                        【解决方案15】:

                        目前 Microsoft 不建议将 Windows 资源管理器与 bash shell 混合使用。在最新的 win10 Insider 版本中,您可以从 bash 中使用类似这样的东西

                        cmd.exe /c start .
                        

                        如果您使用的是 Win10 周年纪念版,您可以尝试安装桌面环境。开始阅读本文 https://github.com/microsoft/bashonwindows/issues/637 之后,您可以打开一个包含当前文件夹内容的窗口

                        gnome-open .
                        

                        【讨论】:

                        • 似乎无法从 WSL bash 执行 cmd.exe(或任何其他 windows .exe 文件),您是否真的对此进行了测试,还是您在猜测?当我输入“cmd.exe /c start”。我收到“执行格式错误”。
                        • 我刚刚在最新的内部版本 14971 上进行了测试,它可以工作,但它总是打开 C:\Windows\System32 文件夹。
                        猜你喜欢
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2017-03-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2015-08-07
                        相关资源
                        最近更新 更多