Signal9Signal9 — home
online
all lessons

Where Am I? Absolute Paths, Relative Paths & ~

Beginner25 min6 steps
What you'll learn
  • 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
  1. 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:\ and D:\ 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/syslog or reports/q1/summary.txt was 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 the root folder, and that is where you are. Whenever a path behaves in a way you did not expect, pwd is 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, and ls lays 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), /home and /root (homes), /var (logs and things that change), /usr (installed programs), /tmp (scratch). The rest are real but specialised — /proc and /sys are live views of the kernel rather than files on a disk, /dev holds devices, /run holds state since the last boot, and /bin, /sbin and 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.sh is 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.
  2. 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_config is 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 /etc jumps straight to /etc in a single hop, from wherever you were, because /etc is 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 && pwd just confirms where you ended up.
  3. 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 pwd reports. So ssh means 'the ssh folder 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.txt in your home makes /root/notes.txt; the identical touch notes.txt after you have stepped into /tmp makes /tmp/notes.txt. Same keystrokes, different file. The command did nothing surprising — it did exactly what a relative path always does, which is start from pwd. 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 ssh
    moduli  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 ssh lists /etc/ssh — because the relative path ssh is resolved from pwd, 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 same ls ssh from your home directory instead and it would fail with 'No such file or directory,' because there is no ssh folder there. Nothing about the command changed; only where it was resolved from did.
  4. 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 ../hosts means 'up to /etc, then into hosts' — 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 what pwd is.

    cd /etc/ssh && cd .. && pwd
    /etc
    Go into /etc/ssh, then cd .. climbs one level to its parent, /etc. .. is resolved relative to where you are, like any relative path: from /etc/ssh it means /etc; from /root it would mean /. Same two characters, different destination, decided entirely by pwd.
  5. ~ 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 root that home is /root; for a user named dana it would be /home/dana. So ~/Downloads and /root/Downloads are the same folder written two ways, and cd ~ and cd /root are 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, or cat sees its argument, the ~ is already gone — it has been replaced with /root. ~ is not a special feature of cd; 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. (Bare cd with no argument goes home too — same effect as cd ~.)

    echo ~
    /root
    echo simply prints its arguments — yet here it prints /root, not a literal ~. That is the proof that the shell rewrote ~ into /root before echo ran; echo never saw a tilde at all. This is exactly what happens to the ~ in ~/inbox or ~/.bashrc in 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. cd on 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?

  6. 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.pdf into 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 from pwd; since pwd is /root, Downloads/ is /root/Downloads/ right now.

    They agree — but only because of where you happen to be standing. Walk into /tmp first 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 to Downloads instead 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`.

Recap
  • 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.