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

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

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


当前回答

我的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:

其他回答

您可以先从Downloading Git安装Git,然后打开Git Bash并键入:

which app-name

我在Windows上找到的最好的版本是Joseph Newcomer的“whereis”实用程序,该实用程序可以从他的网站上获得(带有源代码)。

关于“Where is”发展的文章值得一读。

对于Windows XP用户(他们没有内置where命令),我编写了一个“where like”命令作为一个名为whicher的rubygem。

要安装它,请安装Ruby。

Then

gem install whichr

运行方式如下:

C: >其中的命令

Cygwin是一个解决方案。如果您不介意使用第三方解决方案,那么Cygwin就是您的选择。

Cygwin在Windows环境中为您提供了*nix的舒适性(您可以在Windows命令shell中使用它,也可以选择使用*nix shell)。它为您提供了一整套适用于Windows的*nix命令(如),您可以将该目录包含在PATH中。

在PowerShell下,Get命令将在$Env:PATH中的任何位置找到可执行文件。

$ Get-Command eventvwr

CommandType   Name          Definition
-----------   ----          ----------
Application   eventvwr.exe  c:\windows\system32\eventvwr.exe
Application   eventvwr.msc  c:\windows\system32\eventvwr.msc

因为powershell让我们定义别名,可以这样定义。

$ sal which gcm   # short form of `Set-Alias which Get-Command`
$ which foo
...

PowerShell命令不仅仅是可执行文件(.exe、.ps1等)。它们也可以是cmdlet、函数、别名、在$Env:PATHEXT中设置的自定义可执行后缀等。Get Command能够查找并列出所有这些命令(与Bash的类型-a foo非常相似)。仅这一点就比where.exe、which.exe等(通常仅限于查找可执行文件)更好。

仅使用部分名称查找可执行文件

$ gcm *disk*

CommandType     Name                             Version    Source
-----------     ----                             -------    ------
Alias           Disable-PhysicalDiskIndication   2.0.0.0    Storage
Alias           Enable-PhysicalDiskIndication    2.0.0.0    Storage
Function        Add-PhysicalDisk                 2.0.0.0    Storage
Function        Add-VirtualDiskToMaskingSet      2.0.0.0    Storage
Function        Clear-Disk                       2.0.0.0    Storage
Cmdlet          Get-PmemDisk                     1.0.0.0    PersistentMemory
Cmdlet          New-PmemDisk                     1.0.0.0    PersistentMemory
Cmdlet          Remove-PmemDisk                  1.0.0.0    PersistentMemory
Application     diskmgmt.msc                     0.0.0.0    C:\WINDOWS\system32\diskmgmt.msc
Application     diskpart.exe                     10.0.17... C:\WINDOWS\system32\diskpart.exe
Application     diskperf.exe                     10.0.17... C:\WINDOWS\system32\diskperf.exe
Application     diskraid.exe                     10.0.17... C:\WINDOWS\system32\diskraid.exe
...

查找自定义可执行文件

与UNIX不同,在UNIX中,可执行文件是设置了可执行(+x)位的文件,而windows上的可执行文件则是$PATH env中指定的一个目录中的文件。其文件名后缀在$PATHEXT env中命名的变量。变量(默认为.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL)。

作为Get Command,也将此env作为荣誉。变量,它可以扩展为列出自定义可执行文件。例如

$ $Env:PATHEXT="$Env:PATHEXT;.dll;.ps1;.psm1;.py"     # temporary assignment, only for this shell's process

$ gcm user32,kernel32,*WASM*,*http*py

CommandType     Name                        Version    Source
-----------     ----                        -------    ------
ExternalScript  Invoke-WASMProfiler.ps1                C:\WINDOWS\System32\WindowsPowerShell\v1.0\Invoke-WASMProfiler.ps1
Application     http-server.py              0.0.0.0    C:\Users\ME\AppData\Local\Microsoft\WindowsApps\http-server.py
Application     kernel32.dll                10.0.17... C:\WINDOWS\system32\kernel32.dll
Application     user32.dll                  10.0.17... C:\WINDOWS\system32\user32.dll

有关更多选项和示例,请参见获取命令。