命令 "find | grep 'mystring'" 的 Powershell 等价物是什么?
What is the Powershell equivalent of the command "find | grep 'mystring'"?
命令的 Powershell 等价物是什么
find | grep "mystring"
PowerShell 中 find
的等价物是 Get-ChildItem
,grep
的等价物是 Select-String
,所以你有:
Get-ChildItem -Name | Select-String "mystring"
您可以使用这些命令的通用别名(分别为 gci
和 sls
)将其缩短一点:
gci -n | sls "mystring"
如果您不需要跟随文件夹而只是在某个文件夹内搜索,则不需要 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.
另一种选择是在此处将功能添加到您的个人资料中:
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
命令的 Powershell 等价物是什么
find | grep "mystring"
PowerShell 中 find
的等价物是 Get-ChildItem
,grep
的等价物是 Select-String
,所以你有:
Get-ChildItem -Name | Select-String "mystring"
您可以使用这些命令的通用别名(分别为 gci
和 sls
)将其缩短一点:
gci -n | sls "mystring"
如果您不需要跟随文件夹而只是在某个文件夹内搜索,则不需要 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.
另一种选择是在此处将功能添加到您的个人资料中:
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