什么是Powershell等效命令" find | grep' mystring'"?

时间:2015-07-08 21:28:51

标签: powershell terminal

Powershell等同于命令

find | grep "mystring"

3 个答案:

答案 0 :(得分:3)

PowerShell中find的等效值为Get-ChildItem,而grep的等效值为Select-String,因此您拥有:

Get-ChildItem -Name | Select-String "mystring"

您可以使用这些命令的常用别名(分别为gcisls)来缩短它:

gci -n | sls "mystring"

答案 1 :(得分:2)

如果您不必按照文件夹操作并只在特定文件夹中搜索,则不需要find和Get-ChildItem。 您可以像这样指定PATH到Select-String。 (与> grep“mystring”./*)

相同
PS> Select-String "mystring" .\*

Select-String [-Pattern] <String[]> [-Path] <String[]>

 -Path <String[]>
      Specifies the path to the files to be searched. Wildcards are permitted. The default location is the local directory.
      Specify files in the directory, such as "log1.txt", "*.doc", or "*.*". If you specify only a directory, the command fails.

答案 2 :(得分:0)

另一种选择是在此处将功能添加到您的个人资料中:

C:\Users\<username_here>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

添加此代码:

function findgrep() {
  dir -Filter $args[0] -Recurse | %{$_.FullName} | Resolve-Path -Relative
}

然后您可以像这样使用它:

findgrep "*.png"

输出相对于当前目录:

.\Documents\My Web Sites\WebSite1\iis.png
.\Documents\My Web Sites\WebSite1\some.png