7.4 Absolute vs Relative Paths: / and ..
Right, let’s settle this. The single biggest source of confusion and broken scripts when you’re starting out isn’t some complex algorithm—it’s simply not knowing where you are and how to tell the computer where you want to be. It all boils down to two ways of giving directions: absolute and relative.
Think of it like giving someone your address. You could say, “It’s the big red house on 123 Main Street, Anytown, USA.” That works no matter where in the world you’re starting from. That’s an absolute path. It begins with a forward slash /, the root of the entire filesystem, and lists every single directory down to the target.
/home/your_username/Documents/reports/Q4_earnings.pdf
Or, you could give directions based on your current location. “From this coffee shop, turn left, go two blocks, and it’s on the right.” This is a relative path. It only makes sense if you know the coffee shop you’re both in. It does not start with a /.
The shell (your terminal) always has a “current working directory,” which you can check any time with the pwd (print working directory) command. Every command you run is executed from this location. This context is everything.
The Root of All Things: /
The forward slash / is not just a directory separator; it’s the one true starting point. It’s the foundation upon which every single file and directory is built. The absolute path always, always starts here. It’s the ultimate anchor. If your path starts with /, you’ve just told the computer, “Forget where we are. I’m giving you the coordinates from the center of the universe.”
This is why you can run a script from anywhere on the system that references /etc/config.conf and it will always point to the exact same file. It’s unambiguous and reliable. The downside? It’s often long and inflexible. If you move a directory structure around, all those absolute paths inside it break catastrophically.
Your Home Away from Root: ~
Okay, technically this is a special absolute path, but it’s so vital it deserves a mention. The tilde ~ is your home directory. When the shell sees it, it automatically expands it to the absolute path of your user’s home directory (e.g., /home/your_username).
# These two commands do the exact same thing
cat /home/your_username/.bashrc
cat ~/.bashrc
Which one would you rather type? Exactly. Use ~ liberally. It’s a gift from the lazy geniuses who built these tools.
The Here and Now: . and ..
This is where the relative path magic happens. There are two special directory names that exist in every single directory:
.(a single dot): This means “the current directory.” It’s the default context for everything. When you runls, it’s actually shorthand forls .. It’s most commonly used for executing a program in your current directory, because the shell often doesn’t look there for commands by default (a safety feature). Hence the classic./configureor./my_script.sh...(two dots): This means “the parent directory.” It’s your “go up one level” command. This is your escape hatch from any deep directory nest.
Let’s get our hands dirty. Imagine this directory structure:
/home/you/
├── Projects/
│ ├── website/
│ │ └── index.html
│ └── scripts/
│ └── deploy.sh
└── Documents/
└── notes.txt
If your current working directory (pwd) is /home/you/Projects/website, look at how the same target can be reached different ways:
# Absolute Path (always works, from anywhere)
cat /home/you/Documents/notes.txt
# Relative Path (only works from /home/you/Projects/website)
cat ../../Documents/notes.txt
# Let's break that down:
# First `..` takes us up to /home/you/Projects
# Second `..` takes us up to /home/you/
# Then we go down into Documents/ and find notes.txt
Why This Actually Matters: Pitfalls and Power
The classic rookie mistake is mixing these up and accidentally deleting or overwriting something critical. You think you’re safely in a temp directory and run rm -rf * from / because a script had an absolute path instead of a relative one. It’s a rite of passage, but a terrifying one.
Relative paths are the key to portability. If I have a project folder with a src/ and a scripts/ directory, I can write a script that uses ../scripts/config.sh and it will work on my machine, your machine, and the production server, as long as the relative structure of the project is the same. If I used an absolute path like /home/my_weird_username/project/scripts/config.sh, it would break everywhere else.
Best practice? Use relative paths for anything inside a self-contained project. Use absolute paths for referencing fixed system-wide locations (/etc, /usr/bin). And for the love of all that is holy, always double-check your pwd before running a destructive command like rm that uses ... A quick pwd might just save your filesystem.