PowerShell 中是否有类似 GNU locate 命令的东西?

Raz*_*zor 9 powershell windows-8

locate在 Windows 7/8 中是否有等效的 GNU命令?

locate 可以将文件名作为输入,并给出与输入名称类似的文件所在的所有路径作为输出,例如:

locate file-with-long-name.txt
/var/www/file-with-long-name.txt
Run Code Online (Sandbox Code Playgroud)

Jay*_*uzi 7

不,没有与 Linux/GNU 的命令等效的 Windows cmd 或 PowerShell内置locate命令。但是,功能等效项包括 cmd.exedir /s中描述的cmd.exeJKarthik以及这些 PowerShell 选项:

PS> Get-ChildItem -Recurse . file-with-long-name.txt
Run Code Online (Sandbox Code Playgroud)

注意使用., 告诉 PowerShell 从哪里开始搜索。当然,您可以在命令行键入时缩短:

PS> gci -r . file-with-long-name.txt
Run Code Online (Sandbox Code Playgroud)

我经常这样做,所以我在我的个人资料中添加了一个功能:

PS> function gcir { Get-ChildItem -Recurse . @args }
PS> gcir file-with-long-name.txt
Run Code Online (Sandbox Code Playgroud)

这允许通配符,类似于locate

PS> gcir [a-z]ooo*.txt
Run Code Online (Sandbox Code Playgroud)

有关help about_Wildcards更多详细信息,请参阅。也可以这样写Where-Object

PS> gcir | where { $_ -like "[a-z]ooo*.txt"}
Run Code Online (Sandbox Code Playgroud)

locate有一个选项来匹配正则表达式。PowerShell 也是如此:

PS> gcir | where { $_ -match "A.*B" }
Run Code Online (Sandbox Code Playgroud)

PowerShell 支持完整的 .NET 正则表达式。见about_Regular_Expressions

您也可以执行其他类型的查询:

PS> gcir | where { $_.Length -gt 50M }  # find files over 50MB in size
Run Code Online (Sandbox Code Playgroud)

对于大型文件集合,这些方法的性能很慢,因为它只是搜索文件系统。GNUlocate使用数据库。Windows 现在有一个可搜索的数据库,称为Windows 桌面搜索。有一个 WDS 的 API,有人用 PowerShell cmdlet 封装了它,在这里:http : //www.codeproject.com/Articles/14602/Windows-Desktop-Search-Powershell-Cmdlet,允许以下内容:

PS> get-wds “kind:pics datetaken:this month cameramake:pentax” 
Run Code Online (Sandbox Code Playgroud)

具有比 好得多的性能Get-ChildItem,以及这种丰富的查询(和笨拙的语法)。另外,请注意卷曲引号在 PowerShell 中工作正常,因此在复制/粘贴时无需编辑该示例。

也许有人会找到(或编写)允许对 WDS 进行惯用查询的 PowerShell cmdlet。