Navigating Folders
Every shell has a current folder (also called the working directory). Relative paths are resolved from it, new files are created in it, and the prompt usually shows it. Moving around means changing that folder, and the command is called cd (change directory) in all four shells.
The basics
| Task | CMD | PowerShell | bash / zsh |
|---|---|---|---|
| Where am I? | cd (no argument) |
Get-Location / pwd |
pwd |
| Go into a folder | cd projects |
Set-Location projects / cd projects |
cd projects |
| Go up one level | cd .. |
cd .. |
cd .. |
| Go home | cd /d %USERPROFILE% |
cd ~ |
cd or cd ~ |
| Go to the root | cd \ |
cd \ (Windows), cd / |
cd / |
| Back to the previous folder | popd (after pushd) |
cd - |
cd - |
| Change drive | D: or cd /d D:\data |
cd D:\data |
(no drives) |
Windows (CMD)
cd with no argument prints the current folder, so it doubles as CMD's pwd:
@echo off
mkdir projects\site\css
cd projects
cd
cd site\css
cd
cd ..\..
cd
cd \
cdC:\Users\learner\projects
C:\Users\learner\projects\site\css
C:\Users\learner\projects
C:\
Two CMD-specific details:
- Each drive has its own current folder.
cd D:\datachanges D's folder but leaves you on drive C. Usecd /d D:\datato switch drive and folder at once, or type justD:to switch drives. pushdsaves the current folder on a stack and goes somewhere else;popdcomes back. They're perfect in scripts that must return to where they started.
@echo off
mkdir work\logs
pushd work\logs
echo now in: %CD%
popd
echo back in: %CD%now in: C:\Users\learner\work\logs
back in: C:\Users\learner
%CD% is a dynamic variable that always holds the current folder. pushd also accepts network paths like \\server\share, temporarily mapping a drive letter for them.
Windows (PowerShell)
The real cmdlets are Get-Location and Set-Location; pwd and cd are aliases. Set-Location - (cd -) returns to the previous folder, and cd + goes forward again:
New-Item -ItemType Directory projects/site/css -Force | Out-Null
Set-Location projects/site
(Get-Location).Path
cd css
$PWD.Path
cd -
$PWD.Path
cd ../..
$PWD.Path/tmp/projects/site
/tmp/projects/site/css
/tmp/projects/site
/tmp
$PWD is an automatic variable with the current location. PowerShell's Push-Location/Pop-Location (aliases pushd/popd) work just like CMD's.
PowerShell can also cd into places that aren't folders, such as the registry (cd HKCU:\Software) or environment variables (cd Env:), because they're exposed as drives. See PowerShell Providers & Drives.
Linux & macOS (bash/zsh)
cd with no argument goes home; cd - jumps back to the previous folder (and prints it):
mkdir -p projects/site/css
cd projects/site/css
pwd
cd ../..
pwd
cd -
cd
pwd
cd /
pwd/home/learner/projects/site/css
/home/learner/projects
/home/learner/projects/site/css
/home/learner
/
bash also has pushd/popd with a visible stack (dirs):
mkdir -p a b
pushd a > /dev/null
pushd ../b > /dev/null
dirs
popd > /dev/null
popd > /dev/null
pwd~/b ~/a ~
/home/learner
dirs prints the stack with the newest entry first, abbreviating home as ~.
zsh extras
zsh accepts all of the above and adds conveniences you'll enjoy on a Mac:
setopt autopushd pushdsilent
mkdir -p projects/site
cd projects
cd site
dirs -v
cd ~2
pwd0 ~/projects/site
1 ~/projects
2 ~
/home/learner
autopushd: everycdpushes the old folder onto the stack, sodirs -vshows a numbered history andcd ~2jumps to entry 2.autocd(interactive shells only): typing a folder name on its own, such asprojects, changes into it withoutcd.
Options like these usually go in ~/.zshrc (see zsh Essentials).
A safer way to cd in scripts
If a cd fails in a script (the folder doesn't exist), the following commands run in the wrong place, which is dangerous when they delete files. Stop the script when cd fails:
cd /no/such/folder || { echo "cannot enter folder, stopping"; exit 1; }
echo "this line never runs"/w/main.sh: line 1: cd: /no/such/folder: No such file or directory
cannot enter folder, stopping
cd /d C:\no\such\folder || (echo cannot enter folder & exit /b 1)Set-Location C:\no\such\folder -ErrorAction Stop # throws, so the script stopsLearn more: cd & pwd · cd & dir · Paths on Windows, macOS & Linux
Test yourself
In CMD you are on drive C:. Which command switches to D:\data in one step?
cd D:\datacd /d D:\datachdir D:pushd /d D:\data
What does cd - do in bash, zsh and PowerShell?
- Goes up one level
- Goes to the root
- Returns to the previous folder
- Goes home
What does cd with no argument do?
- Goes home in bash and zsh; prints the current folder in CMD
- Goes home in every shell
- Prints the current folder in every shell
- Nothing in any shell