Where Am I? Absolute Paths, Relative Paths & ~
- Picture the filesystem as one tree with a single root, and read any absolute path as a full address from /
- Explain why a relative path is resolved from your current directory, so the same command can mean different places
- Use . and .. deliberately, and know exactly what each refers to
- Read ~ as shell shorthand that expands to your home directory before the command runs, and know cd ~ equals cd $HOME
- Choose absolute vs relative on purpose, and diagnose a 'lost' file with pwd instead of guessing
One tree, one root
See the whole filesystem as a single tree so that 'a path' stops being mysterious.
Linux keeps every file and folder in one tree. There are no separate drives with their own letters the way
C:\andD:\work on Windows — everything hangs off a single starting point called the root, written as a lone slash:/. A USB stick, a second disk, a network share: each one gets attached into that one tree at some folder, not given a world of its own.A path is nothing more than directions through that tree — the list of folders you pass through to reach a file. That is the entire idea. Every argument you have ever handed a command that looked like
/var/log/syslogorreports/q1/summary.txtwas just directions, and the only thing that ever varies is where the directions start counting from. Get that one distinction and paths stop tripping you up for good.pwd/root
print working directory — it answers 'where am I standing right now?' The reply, /root, is itself a path: start at the root/, step into therootfolder, and that is where you are. Whenever a path behaves in a way you did not expect,pwdis the first thing to check, because half of all path confusion is really just being somewhere other than you thought.ls /bin boot dev entrypoint.sh etc home lib lib32 lib64 libx32 media mnt opt proc root run sbin srv sys tmp usr var
This lists the very top of the tree — the children of root itself. Everything on the machine lives somewhere beneath one of these. Notice /root(the administrator's home) and/home(where ordinary users' homes live) are just ordinary folders hanging off/, no different in kind from any other. That is more entries than you need today, andlslays them out in however many columns fit your window, so your screen may wrap differently — the set of names is what matters, not the shape. Only a handful will come up in this lesson:/etc(configuration),/homeand/root(homes),/var(logs and things that change),/usr(installed programs),/tmp(scratch). The rest are real but specialised —/procand/sysare live views of the kernel rather than files on a disk,/devholds devices,/runholds state since the last boot, and/bin,/sbinand the/lib*set hold programs and the libraries they share. You can ignore all of those for now. One name is not a standard part of the tree at all:entrypoint.shis a leftover from the container this lab box runs in — a reminder that what is at/is whatever that particular machine has, not a fixed list to memorise.Absolute paths: the full address
Read and write an absolute path, and know why it means the same thing from anywhere.
An absolute path starts at the root, so it always begins with a leading
/. Because it spells out the route all the way from the top, it is a complete address — it points at exactly one place on the machine, and it means that same place no matter where you happen to be standing when you use it./etc/ssh/sshd_configis that file, full stop, whether you run the command from your home directory, from/tmp, or from the far end of the tree.The tell is simple: if a path begins with `/`, it is absolute. That leading slash is not decoration — it is the instruction 'start counting from the root.' Absolute paths are what you reach for when you want zero ambiguity: scripts, configuration, anything where 'it depends where you are' would be a bug.
cd /etc && pwd/etc
cd /etcjumps straight to/etcin a single hop, from wherever you were, because/etcis an absolute address. Run this same command from any directory on the machine and you land in exactly the same place — that is the whole point of absolute. The&& pwdjust confirms where you ended up.Relative paths: from where you stand
Understand that a relative path is resolved from pwd, so the same words can point at different places.
A relative path has no leading slash. Instead of starting at the root, it starts from your current directory — from whatever
pwdreports. Sosshmeans 'thesshfolder inside wherever I am right now,' and where that actually is depends entirely on where you are standing.This is the single most important idea about paths, because it is the one that quietly bites people: the same relative path resolves to different absolute places depending on your working directory.
touch notes.txtin your home makes/root/notes.txt; the identicaltouch notes.txtafter you have stepped into/tmpmakes/tmp/notes.txt. Same keystrokes, different file. The command did nothing surprising — it did exactly what a relative path always does, which is start frompwd. Relative paths are shorter and are perfect when you are working inside one area; you just have to keep an eye on where 'here' is.ls sshmoduli ssh_config ssh_config.d ssh_host_ecdsa_key ssh_host_ecdsa_key.pub ssh_host_ed25519_key ssh_host_ed25519_key.pub ssh_host_rsa_key ssh_host_rsa_key.pub ssh_import_id sshd_config sshd_config.d
Standing in /etc(from the previous step),ls sshlists/etc/ssh— because the relative pathsshis resolved frompwd, which is/etc. The contents are the SSH server's configuration and its host keys; do not worry about what each file does, the point is which directory got listed. Run the very samels sshfrom your home directory instead and it would fail with 'No such file or directory,' because there is nosshfolder there. Nothing about the command changed; only where it was resolved from did.Two special names: . and ..
Use . (here) and .. (up one) on purpose.
Every directory contains two entries that are always there, and they are relative paths with fixed meanings.
.(a single dot) means this directory — right here...(two dots) means the parent directory, one level up toward the root. They are the joints that let you move around without spelling out full addresses.cd ..steps you up one level; run it twice,cd ../.., and you climb two...also composes into longer routes: from/etc/ssh, the path../hostsmeans 'up to/etc, then intohosts' — i.e./etc/hosts. And.matters more than it looks: when you run a program that lives in your current folder, you often have to write./script.sh, spelling out 'the one here,' precisely because the shell does not search your current directory by default. Both are pure relative paths — they mean nothing until you know whatpwdis.cd /etc/ssh && cd .. && pwd/etc
Go into /etc/ssh, thencd ..climbs one level to its parent,/etc...is resolved relative to where you are, like any relative path: from/etc/sshit means/etc; from/rootit would mean/. Same two characters, different destination, decided entirely bypwd.~ is your home, spelled short
Read ~ as shell shorthand that expands to your home directory before the command runs.
You will type the path to your own home directory constantly — for config files, downloads, keys, scratch work — so the shell gives you a shortcut: `~` (a tilde) stands for your home directory. For
rootthat home is/root; for a user nameddanait would be/home/dana. So~/Downloadsand/root/Downloadsare the same folder written two ways, andcd ~andcd /rootare the same jump.The key thing to understand is when the substitution happens: the shell expands `~` to the full home path before the command ever runs. By the time
cd,mv, orcatsees its argument, the~is already gone — it has been replaced with/root.~is not a special feature ofcd; it is a spelling the shell rewrites for every command. That is why~on its own is really just a convenient alias for the absolute path to your home: once expanded, it is an absolute path. (Barecdwith no argument goes home too — same effect ascd ~.)echo ~/root
echosimply prints its arguments — yet here it prints/root, not a literal~. That is the proof that the shell rewrote~into/rootbeforeechoran;echonever saw a tilde at all. This is exactly what happens to the~in~/inboxor~/.bashrcin any command: it becomes the absolute path to your home first, then the command runs.cd /tmp && cd ~ && pwd/root
From /tmp— or anywhere —cd ~returns you home, because~expanded to the absolute path/root. It behaves like an absolute jump precisely because that is what it becomes.cdon its own would have done the identical thing.Check yourself
You are standing in /var/log. You run: cd ~ . Which directory are you in afterwards?
Same place, three spellings — and how it bites
Choose absolute vs relative deliberately, and recover a misplaced file with pwd.
Put it together with a single ordinary task: move
report.pdfinto your Downloads folder. Standing in your home directory, all three of these do the exact same thing:mv report.pdf /root/Downloads/— absolute destination.mv report.pdf ~/Downloads/— the~expands to/root, so it is the same absolute destination, less typing.mv report.pdf Downloads/— relative destination, resolved frompwd; sincepwdis/root,Downloads/is/root/Downloads/right now.
They agree — but only because of where you happen to be standing. Walk into
/tmpfirst and run the third one,mv report.pdf Downloads/, and it now means/tmp/Downloads/: a different place, and if that folder does not exist the file gets renamed toDownloadsinstead of moved, and it feels like it vanished. The first two would still have worked, because an absolute destination does not care where you are. That is the practical rule of thumb: when it must not depend on where you are standing, spell it absolute (or use `~`); use a relative path only when you are deliberately working from a known `pwd`.
- The filesystem is one tree rooted at /. A path is just directions through it; the only real question is where the directions start counting from.
- Absolute paths begin with / and mean the same place from anywhere. Relative paths have no leading slash and are resolved from your current directory (pwd), so the same command can land in different places.
- . means here and .. means the parent — both are relative, decided by pwd. ~ is shell shorthand the shell expands to your home directory before the command runs, so ~/x and /home-or-root/x are the same absolute place.
- Choose on purpose: spell it absolute (or use ~) when it must not depend on where you stand; use relative only from a known pwd. When something lands in the wrong place, run pwd first.
