PowerShell vs Bash
This pages shows the equivalent of PowerShell for common unix commands related to text processing.
Simple Commands
The following bash commands have PowerShell alias. (but the options may not be the same)
• cd
• pwd
• ls
• pushd
• popd
• cp
• rm
• rmdir
• mv
• cat
• echo
• set
• ps
• kill
• clear
• man
new file
Linux | PowerShell |
---|---|
touch name | new-item name -ItemType "file" |
new dir
Linux | PowerShell |
---|---|
mkdir name |
mkdir name
or new-item name -ItemType directory
|
find program
Linux | PowerShell |
---|---|
which cmd | Get-Command cmd |
file content
Linux | PowerShell |
---|---|
cat fname | Get-Content fname |
cat fname1 fname2 > newFileName | Get-Content fname1, fname2 > newFileName |
head -n 50 fname | Get-Content fname | select -first 50 |
tail -n 50 fname | Get-Content fname | select -last 50 |
unzip
Linux | PowerShell |
---|---|
unzip fname.zip | Expand-Archive fname.zip |
list files
Linux | PowerShell |
---|---|
find . -name "*html" | Get-ChildItem -Recurse -include *html -name |
find . -type d | Get-ChildItem -Directory -Recurse -name |
find . -type f | Get-ChildItem -File -Recurse |
find . -size 0 | Get-ChildItem -recurse | where {$_.length -eq 0} |
find . -name "*~" | Get-ChildItem -Include *~ -Recurse |
delete file
Linux | PowerShell |
---|---|
find . -name "*~" -delete | Get-ChildItem -Recurse -Include *~ | Remove-Item |
search text (grep)
Linux | PowerShell |
---|---|
grep regex *html | select-string *html -pattern regex -CaseSensitive |
compare file
Linux | PowerShell |
---|---|
cmp f1 f2 | diff (cat f1) (cat f2) |
diff f1 f2 | diff (cat f1) (cat f2)
or Compare-Object (Get-Content f1) (Get-Content f2)
|
diff, sort, get column etc
Linux | PowerShell |
---|---|
sort | Sort-Object |
uniq | sort -Unique |
wc | Measure-Object |
empty trash
Clear-RecycleBin
Redirect
# put content in a file echo "some" > myfile.txt echo "some more" >> myfile.txt # append
# put content in a file "some" > myfile.txt "some more" >> myfile.txt # append
Note that, by default, the PowerShell redirect operator ">" creates files with little endian utf-16 encoding, and lines are hard-wrapped at 80 chars, and line ending uses Windows convention of "\r\n" (ascii 13 and 10).
On unixes, the conventional file encoding is utf-8, and lines are not hard-wrapped (sometimes truncated (deleted) silently), and line ending uses "\n" (ascii 10).
To create unix style output, use out-file, like this:
"1'n2'n3" | out-file -Encoding utf8 -width 999000 myfile.txt
However, the line ending used is still "\r\n". To create unix line ending of just "\n", use:
… | Out-String | %{ $_.Replace("`r`n","`n") } | out-file …
However, the end of the file will still contain a "\r".
thanks to Jeffrey Snover of Microsoft for helping on about 10 of the items. (Jeffrey's the inventor of PowerShell)
If you have a question, put $5 at patreon and message me.