【发布时间】:2017-04-14 06:16:53
【问题描述】:
我知道在Mac OS10中,人们使用open .命令打开当前目录。
请问有人知道在 Windows 上的 Bash 下执行相同任务的适当命令吗?
干杯!
【问题讨论】:
-
你的问题是什么意思?想象一下,您正在查找一些 Word 文档。然后你想打开一个Ubuntu应用程序,立即进入Word文档的目录,MS-Word应用程序,...?
我知道在Mac OS10中,人们使用open .命令打开当前目录。
请问有人知道在 Windows 上的 Bash 下执行相同任务的适当命令吗?
干杯!
【问题讨论】:
最简单的方法是编辑.bash_aliases
nano ~/.bash_aliases
# add
alias open='explorer.exe $1'
# save and close: cltr-s, cltr-x
source ~/.bash_aliases
现在,我们可以使用:open . 或 open documents
【讨论】:
要通过 windows 子系统 for linux (WSL) 打开当前目录,请使用:
$ explorer.exe .
【讨论】:
试试这个
$ explorer .
这将适用于 Windows 中的 bash 命令。我正在使用 R studio,并且能够打开目录。
【讨论】:
使用 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'
【讨论】:
在 WSL2 中,open 命令现在正在执行 start 在 Windows 命令中所做的工作。我只是在我的 .bashrc 中放了一个别名
start="open"
现在您可以根据自己的喜好选择start . 或open .。
【讨论】:
您可以使用以下命令: explorer .
【讨论】:
这个命令应该可以做到:
$ explorer
【讨论】:
开始。 - 这相当于 open 。在 bash 中
【讨论】:
我正在使用这个功能:
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’
证明这是有效的:
【讨论】:
wslpath: .: Invalid argument 。
open 不是别名而是函数。当您打开终端时,您可以输入.profile、.bashrc、bash_aliases 等source。其次,wslpath 现在可以将lxfs 和tmpfs 中的路径正确转换为Windows 路径。即Linux 子系统中的路径。
如果start . 不适合你,它本质上与运行explorer.exe . 相同,所以你可以为它创建一个别名,这就是我所做的。
alias start="explorer.exe"
旁注:另一个有用的是BROWSER。 explorer.exe 能够启动您的默认网络浏览器。当您运行打开 Web 浏览器的脚本(例如启动 React.js 开发服务器)时,这会派上用场。
export BROWSER="explorer.exe"
【讨论】:
我已经添加了
alias open='explorer.exe `wslpath -w "$1"`'
到.bashrc 文件
输入open时打开当前文件夹
我正在使用 windows ubuntu 子系统。
【讨论】:
要在所有类型的路径(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 用户帐户名。
【讨论】:
只需输入
start .
它将打开当前目录文件夹。
【讨论】:
/mnt/c/Windows/System32/start: line 1: $'AcrSch2Svc\r': command not found 这种情况我该怎么办?
您现在可以从 bash 子系统调用 explorer.exe。我已经设置了一个别名来使用它。我已将副本添加到剪贴板别名中,以便更好地衡量。
别名:
alias open="explorer.exe"
alias pbcopy="clip.exe"
示例:
cat ~/.ssh/id_rsa.pub | pbcopy
open .
open "D:\\Dir"
open 别名与. 配合得很好,但如果要指定目录,则需要将 Windows 路径传递给它。
【讨论】:
cygpath --windows <path>命令进行转换。 Read more here.
wslpath 是子系统版本
目前 Microsoft 不建议将 Windows 资源管理器与 bash shell 混合使用。在最新的 win10 Insider 版本中,您可以从 bash 中使用类似这样的东西
cmd.exe /c start .
如果您使用的是 Win10 周年纪念版,您可以尝试安装桌面环境。开始阅读本文 https://github.com/microsoft/bashonwindows/issues/637 之后,您可以打开一个包含当前文件夹内容的窗口
gnome-open .
【讨论】: