PowerShell vs Bash

By Xah Lee. Date: . Last updated: .

This pages shows the common tasks in both bash and PowerShell.

Simple Commands

The following bash commands have PowerShell alias. (but the options are not the same)

cdpwdlspushdpopdcprmrmdirmvcatechosetpskillclearman

New File

touch name
ni name

New Directory

mkdir name
mkdir name

Copy Directory

cp -r sourceDir destDir
cp -r sourceDir destDir

Directory Size

# current dir size, in kilo bytes
du . -sk
"{0:N2}" -f ((dir -Recurse | measure -Property Length -sum).sum / 1MB)

find program

which myCommandName
gcm myCommandName

Print File Content

cat fname
cat fname

Print First/Last n Lines of File

head -n 9 fname
cat -totalcount 9 fname
tail -n 9 fname
cat -tail 9 fname

Join Files

cat fname1 fname2 > newFileName
cat fname1, fname2 > newFileName

unzip

unzip fname.zip
Expand-Archive fname.zip

List Files by File Name Extension

find . -name "*html"
dir -Recurse -filter *html

List Backup Files

find . -name "*~"
dir -Recurse -filter *~

List Directories

find . -type d
dir -Directory -Recurse

List Files Not Directory

find . -type f
dir -File -Recurse

List Empty Files

find . -size 0
dir -recurse | where {$_.length -eq 0}

delete file

find . -name "*~" -delete
dir -Recurse -filter *~ | rm

List Files that Contains Search Text

grep myRegex *html
sls *html -pattern myRegex -CaseSensitive

Diff Files

diff f1 f2
diff (cat f1) (cat f2)

sort, uniq

sort
sort
uniq
sort -Unique

word count, line count

wc
measure

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

thanks to Jeffrey Snover of Microsoft for helping on about 10 of the items. (Jeffrey's the inventor of PowerShell)