Paths on Windows, macOS & Linux
A path is the address of a file or folder. Almost every command you type takes one, so it pays to understand how they're written on each system. The ideas are identical everywhere; the spelling differs.
Two kinds of file trees
Windows: one tree per drive macOS and Linux: one single tree
C:\ /
├── Program Files\ ├── bin/ etc/ usr/ var/ tmp/
├── Windows\ ├── home/ (Linux)
└── Users\ │ └── ada/
└── ada\ ← home │ └── projects/
└── projects\ ├── Users/ (macOS)
D:\ │ └── ada/ ← home
└── backup\ └── Volumes/ or /mnt (other disks)| Windows | macOS | Linux | |
|---|---|---|---|
| Separator | \ (backslash; / also accepted by most programs) |
/ |
/ |
| Root | one per drive: C:\, D:\ |
/ |
/ |
| Home folder | C:\Users\ada |
/Users/ada |
/home/ada |
| Home shortcut | %USERPROFILE% (CMD), ~ or $HOME (PowerShell) |
~, $HOME |
~, $HOME |
| Other disks | another letter (D:\) |
under /Volumes |
under /mnt or /media |
| Case-sensitive names? | no | no (by default) | yes |
| Network share | \\server\share\file |
smb:// mounted under /Volumes |
mounted under /mnt |
The OS course explains the two layouts in depth: The Windows Folder Layout and The Linux Directory Tree.
Absolute and relative paths
An absolute path starts at the root and works from anywhere: C:\Users\ada\notes.txt, /home/ada/notes.txt.
A relative path starts at the current folder (the working directory) and is shorter: notes.txt, projects\site\index.html, ../photos. Two special names exist in every folder on every system:
| Name | Means |
|---|---|
. |
this folder |
.. |
the parent folder (one level up) |
So from /home/ada/projects, the path ../photos/cat.jpg means /home/ada/photos/cat.jpg.
On Windows, a path starting with \ (no drive letter) is relative to the root of the current drive, and C:notes.txt (no backslash) is relative to the current folder on drive C. You rarely need these, but they explain some surprises.
Splitting and joining paths
Every shell has tools to take a path apart: the folder part, the file name, and the extension.
Windows (CMD)
In batch files, for variables and arguments accept modifiers such as ~d (drive), ~p (path), ~n (name), ~x (extension) and ~f (full path):
@echo off
mkdir reports
echo data> reports\sales.2025.csv
for %%f in (reports\sales.2025.csv) do (
echo full: %%~ff
echo drive: %%~df
echo folder: %%~dpf
echo name: %%~nf
echo extension: %%~xf
)full: C:\Users\learner\reports\sales.2025.csv
drive: C:
folder: C:\Users\learner\reports\
name: sales.2025
extension: .csv
Only the last dot starts the extension, so the name is sales.2025.
Windows (PowerShell)
PowerShell has cmdlets for this, and they handle the separator for you, so the same script works on Windows and Unix:
$p = Join-Path 'reports' 'sales.2025.csv'
$p
Split-Path $p -Parent
Split-Path $p -Leaf
Split-Path $p -LeafBase
Split-Path $p -Extension
[System.IO.Path]::GetFullPath('reports/../notes.txt')reports/sales.2025.csv
reports
sales.2025.csv
sales.2025
.csv
/tmp/notes.txt
On Windows, Join-Path would produce reports\sales.2025.csv and the full path would start with C:\. PowerShell on Windows accepts / as well as \.
Linux & macOS (bash/zsh)
dirname and basename are small programs; parameter expansion cuts strings without starting a program. realpath turns a relative path into an absolute one:
mkdir -p reports
p="reports/sales.2025.csv"
dirname "$p"
basename "$p"
basename "$p" .csv
echo "extension: ${p##*.}"
echo "without extension: ${p%.*}"
realpath reports/../notes.txtreports
sales.2025.csv
sales.2025
extension: csv
without extension: reports/sales.2025
/home/learner/notes.txt
${p##*.} removes everything up to the last dot; ${p%.*} removes the last dot and what follows. The same expansions work in zsh, which also offers short modifiers: :h (head = folder), :t (tail = name), :r (root = without extension), :e (extension) and :a (absolute):
p="reports/sales.2025.csv"
print "head: $p:h"
print "tail: $p:t"
print "root: $p:r"
print "ext: $p:e"
print "abs: ${p:a}"head: reports
tail: sales.2025.csv
root: reports/sales.2025
ext: csv
abs: /home/learner/reports/sales.2025.csv
Spaces and special characters
A space separates arguments, so a path containing spaces must be quoted (or the space escaped):
mkdir "My Projects"
cd "My Projects" && pwd
cd ..
cd My\ Projects && pwd/home/learner/My Projects
/home/learner/My Projects
@echo off
mkdir "My Projects"
cd "My Projects"
cdC:\Users\learner\My Projects
Windows also forbids some characters in names (\ / : * ? " < > |) and some names entirely (CON, NUL, PRN, COM1, LPT1 …). Linux forbids only / and the zero byte; macOS's Finder avoids :. For files you share between systems, stick to letters, digits, -, _ and ..
Tip: Press Tab while typing a path and the shell completes it, adding quotes or escapes for spaces automatically. On macOS you can also drag a file from Finder into the Terminal window to paste its path.
Comparison
| Task | CMD | PowerShell | bash / zsh |
|---|---|---|---|
| Current folder | cd |
Get-Location (pwd) |
pwd |
| Home folder | %USERPROFILE% |
$HOME, ~ |
$HOME, ~ |
| Parent folder | .. |
.. |
.. |
| Join two parts | %dir%\%file% |
Join-Path a b |
"$dir/$file" |
| Folder part | %%~dpf |
Split-Path -Parent |
dirname, ${p%/*}, zsh $p:h |
| File name | %%~nxf |
Split-Path -Leaf |
basename, ${p##*/}, zsh $p:t |
| Make absolute | %%~ff |
Resolve-Path, [IO.Path]::GetFullPath() |
realpath, zsh ${p:a} |
Learn more: cd & pwd · PowerShell Providers & Drives · Arguments: %1 & %~
Test yourself
You are in /home/ada/projects/site. Where does ../../notes.txt point?
/home/ada/projects/notes.txt/home/ada/notes.txt/home/notes.txt/notes.txt
Which statement about file names is true?
- Linux treats
Report.txtandreport.txtas the same file - Windows treats
Report.txtandreport.txtas the same file - macOS forbids spaces in file names
- Windows forbids dots in folder names
In a batch file, %%f holds C:\data\photo.jpg. What does %%~xf give?
photo.jpgC:C:\data\