由于我有时会遇到路径问题,我自己的一个cmd脚本被另一个程序隐藏(隐藏)(路径前面),所以我希望能够在Windows命令行上找到程序的完整路径,只要给出它的名称。

是否有与UNIX命令“which”等效的命令?

在UNIX上,哪个命令打印给定命令的完整路径,以便轻松查找和修复这些阴影问题。


当前回答

只需发布此Windows的单行批处理文件:

C:>type wh.cmd
@for %%f in (%*) do for %%e in (%PATHEXT% .dll .lnk) do for %%b in (%%f%%e) do for %%d in (%PATH%) do if exist %%d\%%b echo %%d\%%b

A测试:

C:>wh ssh
C:\cygwin64\bin\ssh.EXE
C:\Windows\System32\OpenSSH\\ssh.EXE

如果将代码包装在setlocalenableextensions和endlocal中,那就不是一行了。

其他回答

只需发布此Windows的单行批处理文件:

C:>type wh.cmd
@for %%f in (%*) do for %%e in (%PATHEXT% .dll .lnk) do for %%b in (%%f%%e) do for %%d in (%PATH%) do if exist %%d\%%b echo %%d\%%b

A测试:

C:>wh ssh
C:\cygwin64\bin\ssh.EXE
C:\Windows\System32\OpenSSH\\ssh.EXE

如果将代码包装在setlocalenableextensions和endlocal中,那就不是一行了。

我的PowerShell配置文件中有一个名为“which”的函数

function which {
    get-command $args[0]| format-list
}

输出如下:

PS C:\Users\fez> which python


Name            : python.exe
CommandType     : Application
Definition      : C:\Python27\python.exe
Extension       : .exe
Path            : C:\Python27\python.exe
FileVersionInfo : File:             C:\Python27\python.exe
                  InternalName:
                  OriginalFilename:
                  FileVersion:
                  FileDescription:
                  Product:
                  ProductVersion:
                  Debug:            False
                  Patched:          False
                  PreRelease:       False
                  PrivateBuild:     False
                  SpecialBuild:     False
                  Language:

可以下载为Windows编译的所有UNIX命令,包括从GitHub存储库下载的命令:https://github.com/George-Ogden/UNIX

JPSoft的TCC和TCC/LE是CMD.EXE的替代品,增加了重要的功能。与OP的问题相关,这是TCC系列命令处理器的内置命令。

我已经使用npm中的which模块很长时间了,它工作得很好:https://www.npmjs.com/package/which这是一个很好的多平台替代方案。

现在我切换到Git附带的。只需将Git中的/usr/bin路径添加到路径中,该路径通常位于C:\Program Files\Git\usr\bin\which.exe。哪个二进制文件位于C:\Program Files \Git\usr/bin\which.exe。它速度更快,也能按预期工作。