Command Line Basics

Expected level: Proficient

The command line is the fastest way to move around, run tools and automate repeated work. On a Windows workstation you will meet three shells: Command Prompt (cmd), PowerShell and Bash.

Terminal vs shell

The shell interprets your commands (cmd, PowerShell, Bash). The terminal is the window that hosts it. Windows Terminal can host any of them in tabs, including a WSL distribution. The classic console window works too, with fewer features.

Same task, three shells

Task Command Prompt PowerShell Bash
Where am I? cd Get-Location (pwd) pwd
List files dir Get-ChildItem (ls, dir) ls -l
Change folder cd src Set-Location src (cd) cd src
Make folder mkdir out New-Item -ItemType Directory out (mkdir) mkdir -p out
Create an empty file type nul > f.txt New-Item f.txt touch f.txt
Copy / move / delete a file copy, move, del Copy-Item, Move-Item, Remove-Item cp, mv, rm
Copy a folder with contents xcopy src dst /e /i Copy-Item src dst -Recurse cp -r src dst
Delete a folder with contents rmdir /s out Remove-Item out -Recurse rm -r out
Show a file type f.txt Get-Content f.txt cat f.txt
Find text findstr err f.txt Select-String err f.txt grep err f.txt
Count lines find /c /v "" < f.txt Get-Content f.txt | Measure-Object -Line wc -l f.txt
List by size, largest first dir /o-s Get-ChildItem | Sort-Object Length -Descending ls -lS
Help help dir Get-Help Get-ChildItem man ls

PowerShell has short aliases such as ls and cat. They are not the Bash programs, so options like -la do not work there. Bash and Command Prompt pass text; PowerShell passes .NET objects. For large copies robocopy src dst /e is the more robust Windows tool. rmdir /s asks for confirmation unless you add /q.

To get a Bash shell on a Windows workstation, use Git Bash (installed with Git for Windows) or a WSL distribution.

Pipes and redirection

command > out.txt     # write output to a file (overwrite)
command >> out.txt    # append
command 2> err.txt    # send error output to a file
command1 | command2   # feed the output of one into the next

The same operators work in all three shells, but the commands on either side differ:

dir > out.txt
dir | findstr ".md"
Get-ChildItem > out.txt
Get-ChildItem | Where-Object Length -gt 1MB | Sort-Object Length
ls > out.txt
ls | grep ".md"

Shortcuts and cautions

  • Press Tab to complete names, and the Up arrow (or Ctrl+R in Bash) for history.
  • Quote paths that contain spaces with double quotes in all three shells: cd "C:\Program Files". In PowerShell, running a quoted program path needs the call operator: & "C:\Program Files\app\app.exe".
  • Relative paths start from the current folder, absolute ones from the root. . is the current folder, .. its parent.
  • Ctrl+C stops a running command.
  • Check which program runs with where.exe git (cmd/PowerShell) or which git (Bash).
  • Be careful with rm -r, rmdir /s and Remove-Item -Recurse: there is no recycle bin. Check where you are and list the folder first.

Try it

In each of the three shells, create a folder, add a file with text in it, search the file, list the folder sorted by size and delete everything again. Then redirect a directory listing into a file and count its lines.

Common mistakes

  • Using Bash options such as `ls -la` in PowerShell, where `ls` is only an alias.
  • Running `rm -r`, `rmdir /s` or `Remove-Item -Recurse` without checking the current folder; there is no recycle bin.
  • Leaving paths with spaces unquoted.
  • Running a quoted program path in PowerShell without the `&` call operator.

Check yourself

  • What is the difference between a terminal and a shell?
  • How do you append output to a file, and how do you send only error output to one?
  • Which command shows which program runs when you type `git`, in cmd and in Bash?

Related topics

Terms

Further reading