00 /What a terminal is
A terminal is a window where you type commands instead of clicking. The terminal always works "inside" one folder, the current folder. Commands that use files look there first.
Open one: on Windows search for PowerShell (or Git Bash if you installed Git). On a Mac open Terminal. On Linux, the keyboard shortcut is usually Ctrl+Alt+T.
01 /Moving around and looking at files
| You want to | Bash (Mac, Linux, Git Bash) | PowerShell (Windows) |
|---|---|---|
| Where am I? | pwd | Get-Location (or pwd) |
| List files | ls | dir (or ls) |
| Go into a folder | cd lab-folder | cd lab-folder |
| Go up one folder | cd .. | cd .. |
| Make a folder | mkdir mylab | mkdir mylab |
| Show a file | cat file.txt | type file.txt (or cat) |
| Copy, move, delete a file | cp a b, mv a b, rm a | Copy-Item a b, Move-Item a b, Remove-Item a |
| Delete a folder and everything in it | rm -rf folder | Remove-Item -Recurse -Force folder |
There is no recycle bin. rm -rf and Remove-Item -Recurse -Force delete for good, and the folder name must be exactly right.
02 /Starting a lab
mkdir my-lab && cd my-lab
unzip ~/Downloads/my-lab.zip
lsmkdir my-lab; cd my-lab
Expand-Archive -Path "$HOME/Downloads/my-lab.zip" -DestinationPath .
dirUnzip every lab into its own new folder, then run all commands from inside that folder. Many "file not found" errors are only "I am in the wrong folder".
03 /Running scripts
| You want to | Bash | PowerShell |
|---|---|---|
| Run a script in this folder | ./test.sh | .\test.ps1 |
| Run Python | python3 file.py | python file.py (or py file.py) |
| Stop a running command | Ctrl+C | Ctrl+C |
| Repeat the last command | Up arrow, Enter | Up arrow, Enter |
| Clear the screen | clear | cls |
If PowerShell refuses to run a .ps1 file, run it like this once: powershell -ExecutionPolicy Bypass -File .\test.ps1.
04 /Pipes, searching and saving output
| You want to | Bash | PowerShell |
|---|---|---|
| Send the output of one command into another (a pipe) | docker ps | grep web | docker ps | Select-String web |
| Search a file for text | grep error app.log | Select-String error app.log |
| Show the first or last lines | head -20 f, tail -20 f | Get-Content f -TotalCount 20, Get-Content f -Tail 20 |
| Save output to a file | docker compose logs > logs.txt | docker compose logs > logs.txt |
| Count lines | wc -l file | (Get-Content file).Count |
05 /Environment variables
| You want to | Bash | PowerShell |
|---|---|---|
| Set one for the current terminal | export REDIS_HOST=cache | $env:REDIS_HOST = "cache" |
| Set one for a single command | REDIS_HOST=cache docker compose up -d | $env:REDIS_HOST = "cache"; docker compose up -d; Remove-Item Env:REDIS_HOST |
| Read one | echo $REDIS_HOST | echo $env:REDIS_HOST |
06 /Quotes and long lines
- Bash: single quotes
'...'keep the text exactly as typed. Double quotes"..."allow$variables. - PowerShell: the same idea, but when you pass JSON with double quotes to a program such as
curl.exe, put a backslash before each inner quote:-d '{\"name\":\"asha\"}'. - Long lines: end the line with a backslash in Bash, and with a backtick in PowerShell, then continue on the next line.
07 /Three Windows traps
curlin PowerShell is a different command. Typecurl.exe.python3on Windows may open the Microsoft Store. Usepythonorpy.- In Git Bash, a path like
/appinside a command can be rewritten toC:/Program Files/Git/app. PutMSYS_NO_PATHCONV=1in front of the command, or use PowerShell.
More fixes are in the Windows Troubleshooting guide.