🙆
[PowerShell]PSでLinuxの「find」コマンド①
簡単な文法おさらい
記法 | PowerShell | Linux |
---|---|---|
フォルダ内のファイル一覧表示 | Get-ChileItem (-Force) | ls (-la) |
余計なエラーログを除外 | -ErrorAction SilentlyContinue | 2>/dev/null |
再帰的に検索 | -Recurse | -R |
タイプ指定 | -File | -f |
ファイル名指定 | -Filter | -name |
PowerShellでLinuxのfindコマンドもどきを実施
・一般的なコマンド
【Linux】
find / -name "hosts"
【PS】
Get-ChildItem -Path "/" -Recurse -File -Filter "hosts"
実行時に余計なエラー表示がでてしまう
PS C:\Users\hogehoge> Get-ChildItem -Path "/" -Recurse -File -Filter "hosts"
ディレクトリ: C:\Program Files\Git\etc
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2019/12/07 18:12 824 hosts
Get-ChildItem : パス 'C:\Program Files\Windows Defender Advanced Threat Protection\Classification\Configuration' へのア
クセスが拒否されました。
発生場所 行:1 文字:1
+ Get-ChildItem -Path "/" -Recurse -File -Filter "hosts"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Program File...n\Configuration:String) [Get-ChildItem], Unauthoriz
edAccessException
+ FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
・エラー除外
Linuxでいう、「2>/dev/null」を実現するには
「-ErrorAction SilentlyContinue」
を指定すればよい。
Get-ChildItem -Path "/" -Recurse -File -ErrorAction SilentlyContinue -Filter "hosts"
PS C:\Users\hogehoge> Get-ChildItem -Path "/" -Recurse -File -ErrorAction SilentlyContinue -Filter "hosts"
ディレクトリ: C:\Program Files\Git\etc
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2019/12/07 18:12 824 hosts
ディレクトリ: C:\Windows\System32\drivers\etc
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2025/01/31 10:52 1159 hosts
ディレクトリ: C:\Windows\WinSxS\amd64_microsoft-windows-w..ucture-other-minwin_31bf3856ad364e35_10.0.26100.1_none_3
23345d6cd9e8cb7
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2024/04/01 16:22 824 hosts
Discussion