TExcelTAD
Home › Terminal & Command Line › Command Cheat Sheet

Command Cheat Sheet

This page is a "Rosetta stone" for the command line: the same task in Windows CMD, PowerShell and bash/zsh (Linux and macOS), side by side. Use it when you know how to do something in one shell and need it in another. PowerShell entries use the full cmdlet name; the common alias is in brackets. Each row links back to the lesson that explains it.

Navigation & information

Task CMD PowerShell bash / zsh
Where am I? cd Get-Location (pwd) pwd
Change folder cd /d D:\work Set-Location D:\work (cd) cd ~/work
Up one level cd .. cd .. cd ..
Home folder cd %USERPROFILE% cd ~ cd or cd ~
Previous folder popd (after pushd) cd - (PS 7) cd -
List files dir Get-ChildItem (ls, dir) ls -l
Include hidden dir /a Get-ChildItem -Force ls -a
List recursively dir /s /b Get-ChildItem -Recurse find . or ls -R
Clear screen cls Clear-Host (cls) clear or Ctrl+L
Help for a command dir /? Get-Help Get-ChildItem man ls, ls --help
Who am I whoami whoami, $env:USERNAME whoami
Computer name hostname hostname, $env:COMPUTERNAME hostname
OS version ver $PSVersionTable, [Environment]::OSVersion uname -a, cat /etc/os-release, sw_vers (macOS)

Lessons: Navigating Folders, Listing Files, Getting Help.

Files & folders

Task CMD PowerShell bash / zsh
Make folder mkdir a\b New-Item -ItemType Directory a/b (mkdir) mkdir -p a/b
Empty file type nul > f.txt New-Item f.txt touch f.txt
Copy file copy a.txt b.txt Copy-Item a.txt b.txt (cp) cp a.txt b.txt
Copy folder xcopy src dst /e /i or robocopy src dst /e Copy-Item src dst -Recurse cp -r src dst
Move / rename move a.txt dir\ / ren a.txt b.txt Move-Item (mv) / Rename-Item mv a.txt dir/ / mv a.txt b.txt
Delete file del f.txt Remove-Item f.txt (rm) rm f.txt
Delete folder rd /s /q dir Remove-Item dir -Recurse rm -r dir
Show file type f.txt Get-Content f.txt (cat) cat f.txt
Page through more f.txt Get-Content f.txt piped to more less f.txt
First 5 lines — Get-Content f.txt -TotalCount 5 head -n 5 f.txt
Last 5 lines — Get-Content f.txt -Tail 5 tail -n 5 f.txt
Follow a log — Get-Content log.txt -Wait tail -f log.txt
Compare files fc a.txt b.txt Compare-Object (gc a.txt) (gc b.txt) diff a.txt b.txt
Find files by name dir /s /b *.log Get-ChildItem -Recurse -Filter *.log find . -name "*.log"
Zip a folder tar -a -cf out.zip dir Compress-Archive dir out.zip zip -r out.zip dir
Open with default app start f.pdf Invoke-Item f.pdf (ii) xdg-open f.pdf (Linux), open f.pdf (macOS)
Open folder in file manager explorer . explorer . xdg-open ., open .

Lessons: Creating Files & Folders, Copy, Move & Rename, Deleting Safely, Viewing File Contents.

Searching & text

Task CMD PowerShell bash / zsh
Find text in a file findstr "error" app.log Select-String error app.log (sls) grep error app.log
Ignore case findstr /i (default) grep -i
Search all files below findstr /s /i "todo" *.js Get-ChildItem -Recurse *.js piped to Select-String todo grep -ri todo --include="*.js" .
Count lines find /c /v "" f.txt (Get-Content f.txt).Count wc -l f.txt
Sort lines sort f.txt Get-Content f.txt piped to Sort-Object sort f.txt
Unique lines — Sort-Object -Unique sort -u
Replace text — (Get-Content f) -replace 'a','b' sed 's/a/b/g' f
Parse JSON — Get-Content f.json piped to ConvertFrom-Json jq . f.json

Lessons: Searching Files & Text, Pipes, Wildcards & Globbing.

Shell features

Task CMD PowerShell bash / zsh
Output to file cmd > f.txt cmd > f.txt cmd > f.txt
Append cmd >> f.txt cmd >> f.txt cmd >> f.txt
Errors to file cmd 2> err.txt cmd 2> err.txt cmd 2> err.txt
Discard output cmd > nul cmd > $null, Out-Null cmd > /dev/null
Run if OK / if failed a && b / a || b a && b / a || b (PS 7) a && b / a || b
Run one after another a & b a; b a; b
Set variable set NAME=Ada $name = 'Ada' name=Ada
Use variable %NAME% $name $name
Env variable set NAME=Ada $env:NAME = 'Ada' export NAME=Ada
Show all env vars set Get-ChildItem env: env or printenv
Show PATH echo %PATH% $env:PATH -split ';' echo $PATH
Which program runs? where python Get-Command python (gcm) which python3, type python3
Command history doskey /history, F7 Get-History (h), Ctrl+R history, Ctrl+R
Last exit code %ERRORLEVEL% $LASTEXITCODE, $? $?
Comment rem text or :: text # text # text
Make an alias doskey ll=dir $* Set-Alias ll Get-ChildItem alias ll='ls -l'
Profile file (AutoRun registry key) $PROFILE ~/.bashrc, ~/.zshrc

Lessons: Redirection, Chaining Commands, Variables & Environment Variables, Aliases & Profiles.

Processes, system & network

Task CMD PowerShell bash / zsh
List processes tasklist Get-Process (ps) ps aux, top
Kill by name taskkill /im app.exe Stop-Process -Name app pkill app
Kill by PID taskkill /pid 1234 /f Stop-Process -Id 1234 -Force kill -9 1234
Run in background start /b cmd Start-Job { cmd } cmd &
Wait / pause timeout /t 5 Start-Sleep 5 sleep 5
Run as admin Run as administrator Start-Process pwsh -Verb RunAs sudo cmd
Disk space dir (last line) Get-PSDrive C df -h
Services sc query Get-Service systemctl list-units --type=service (Linux)
IP address ipconfig Get-NetIPAddress ip addr (Linux), ifconfig (macOS)
Ping ping -n 4 host Test-Connection host ping -c 4 host
DNS lookup nslookup host Resolve-DnsName host dig host, nslookup host
HTTP request curl.exe url Invoke-RestMethod url curl url
Listening ports netstat -ano Get-NetTCPConnection -State Listen ss -tlnp (Linux), lsof -i -P (macOS)
Install software winget install X winget install X sudo apt install X, brew install X
Remote shell ssh user@host ssh user@host ssh user@host

Lessons: Processes & Jobs, Admin Rights & Permissions, Network Commands, Package Managers.

Scripting basics

Task Batch (.bat) PowerShell (.ps1) bash (.sh)
First line @echo off param(...) (optional) #!/usr/bin/env bash
Print echo Hi Write-Output 'Hi' or just 'Hi' echo Hi
First argument %1 $args[0] or a param() $1
If if "%x%"=="a" (...) else (...) if ($x -eq 'a') {...} else {...} if [ "$x" = a ]; then ...; else ...; fi
File exists? if exist f.txt if (Test-Path f.txt) if [ -e f.txt ]
Loop over files for %%f in (*.txt) do echo %%f foreach ($f in Get-ChildItem *.txt) {...} for f in *.txt; do echo "$f"; done
Count 1 to 3 for /l %%i in (1,1,3) do echo %%i foreach ($i in 1..3) {...} for i in {1..3}; do echo $i; done
Arithmetic set /a n=n+1 $n++ ((n++))
Function call :name arg ... :name function Name { ... } name() { ... }
Exit with code exit /b 1 exit 1 exit 1

Here is the "loop over files" row as working code, the same tiny task in all three shells: loop over the text files in a folder and report how many there are.

cmd · try it yourself
@echo off
mkdir notes
cd notes
echo a> a.txt
echo b> b.txt
echo c> c.log
set count=0
for %%f in (*.txt) do (
  echo found %%f
  set /a count+=1
)
call echo %%count%% text files
found a.txt
found b.txt
2 text files
powershell · try it yourself
New-Item -ItemType Directory notes | Out-Null
Set-Location notes
'a' > a.txt; 'b' > b.txt; 'c' > c.log
$files = Get-ChildItem *.txt | Sort-Object Name
foreach ($f in $files) { "found $($f.Name)" }
"$($files.Count) text files"
found a.txt
found b.txt
2 text files
bash · try it yourself
mkdir notes && cd notes
echo a > a.txt; echo b > b.txt; echo c > c.log
count=0
for f in *.txt; do
  echo "found $f"
  count=$((count + 1))
done
echo "$count text files"
found a.txt
found b.txt
2 text files

(The batch version uses call echo %%count%% because of how CMD expands variables inside a ( ) block; see SETLOCAL & Delayed Expansion.)

Learn more: Your First Script on Each OS, Cross-Platform Scripts.

Test yourself

What's the bash/zsh equivalent of CMD's findstr and PowerShell's Select-String?

  1. find
  2. grep
  3. sed
  4. awk

Which command shows the contents of a file in CMD?

  1. cat
  2. type
  3. more -f
  4. show

How do you set an environment variable for child programs in bash?

  1. set NAME=Ada
  2. $env:NAME = 'Ada'
  3. export NAME=Ada
  4. env NAME