はじめに
こんにちは!
Linuxで作業しているときに使っていたコマンドを、Windowsで同じように使いたいことがありますよね!
今回は、Linuxでよく使うコマンドをPowerShellでどのように置き換えるかを整理してみました。
LinuxからWindowsへ
基本の対応表
Linuxコマンド | PowerShellコマンド | 主な用途 |
---|---|---|
cat |
Get-Content |
ファイル内容の表示 |
tail |
Get-Content -Tail n |
ファイル末尾n行を表示 |
head |
Get-Content -TotalCount n |
ファイル先頭n行を表示 |
tail -f |
Get-Content -Wait |
ファイル追記監視 |
tail -F |
代替無し | ローテート後も監視 |
grep |
Select-String |
テキスト検索 |
ls , dir |
Get-ChildItem または ls /dir |
ディレクトリ一覧 |
find |
Get-ChildItem |
ファイル探索 |
pwd |
Get-Location |
カレントディレクトリ表示 |
ps |
Get-Process |
プロセス一覧表示 |
top |
Get-Process + ループ |
動的なプロセス監視 |
netstat |
netstat または Get-NetTCPConnection |
ネットワーク接続一覧 |
touch |
New-Item |
空ファイル作成 |
rm |
Remove-Item |
ファイル/ディレクトリ削除 |
mv |
Move-Item |
移動/リネーム |
cp |
Copy-Item |
コピー |
具体例
ファイル末尾をn行だけ表示(tail -n)
Get-Content .\hoge.log -Tail 20
テキスト検索(grepの代替)
Select-String "error" .\hoge.log
ファイル監視(tail -F + grep の置き換え)
Get-Content .\hoge.log -Wait | Select-String "test"
プロセス確認(top -n 1 の代替)
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10
プロセス監視(top の代替)
while ($true) { Clear-Host; Get-Process | Sort-Object CPU -Descending | Select-Object -First 10; Start-Sleep 2 }
grep 代替コマンドの詳細
grep
の各オプションの代替についてまとめておきました。
grep オプション | Linuxでの意味 | PowerShellでの書き方 | 備考 |
---|---|---|---|
grep パターン |
指定パターンを含む行を表示 | Select-String "パターン" ファイル名 |
正規表現で検索(デフォルト) |
-i |
大文字小文字を区別しない | Select-String -CaseSensitive:$false ... |
省略時デフォルトで区別しない |
-v |
パターンに一致しない行を表示 | Select-String -NotMatch ... |
否定検索 |
-n |
行番号を表示 | Select-String "error" .\hoge.log | Select-Object LineNumber, Line |
– |
-E |
拡張正規表現 | デフォルトで正規表現対応 | – |
-F |
固定文字列検索(正規表現を使わない) | Select-String -SimpleMatch ... |
– |
find 代替コマンドの詳細
find
の各オプションの代替についてまとめておきました。
Linuxコマンド例 | 意味 | PowerShellでの書き方例 |
---|---|---|
find . -name "*.txt" |
カレント以下のすべての.txtファイルを探す | Get-ChildItem -Path . -Filter *.txt -Recurse |
find /tmp -type d |
/tmp以下のディレクトリのみ探す | Get-ChildItem /tmp -Directory -Recurse |
find . -type f -size +1M |
1MB以上のファイルを探す | Get-ChildItem -Recurse -File | Where-Object { $_.Length -gt 1MB } |
find . -mtime -1 |
1日以内に更新されたファイルを探す | Get-ChildItem -Recurse | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-1) } |
おわりに
最後まで読んでいただき、ありがとうございます。
WindowsでLinuxのようにコマンド操作したい方の参考になれば幸いです。
コメント