Day 1: Absolute vs. Relative Paths
Note: We briefly introduced this concept in the previous section when learning cd, but because this is the #1 concept that trips up beginners, we are going to dive a bit deeper into the exact mechanics of how paths work.
When you are navigating the terminal, you are constantly giving the shell directions. If you tell the shell to delete a file, copy a folder, or move to a new directory, you have to tell it exactly where that item is.
There are only two ways to give the shell directions: Absolute Paths and Relative Paths.
Understanding the difference between the two is the difference between writing a command that works perfectly and getting a frustrating No such file or directory error.
The Analogy: Mailing Addresses vs. Street Directions
-
An Absolute Path is like a full mailing address: 123 Main Street, Springfield, IL 62701. No matter where you are in the world, if you hand that address to a postman, they know exactly how to find the house.
-
A Relative Path is like giving a friend street directions: “Go out the front door, take a left, and it’s the second house on the right.” These directions are completely useless unless your friend is already standing in your living room.
1. The Absolute Path
An absolute path traces the location of a file or folder all the way from the absolute top of the server—the Root directory (/).
The Rule: An absolute path always begins with a forward slash (/).
Example: /home/student/docs If you type cd /home/student/docs, the shell goes to the Root (/), looks for the home folder, looks inside that for student, and finally looks inside that for docs. It does not matter if your current location is deep inside /var/log/nginx or sitting in /etc; this command will teleport you to the exact same place every time.
2. The Relative Path
A relative path gives directions starting from your Current Working Directory (wherever pwd says you are standing right now).
The Rule: A relative path never begins with a forward slash.
Example 1: Moving Forward (docs) If you type cd docs, the shell looks at the room you are currently standing in. If there is a folder named docs right in front of you, it steps inside. If there isn’t, the command fails.
Example 2: Moving Backward (../docs) Remember the “double dot” (..) shortcut from the last lesson? It means “go up one level.” Imagine your current location is /home/student/pictures. If you type cd ../docs, you are giving the shell a two-step relative direction:
-
..means “step backward out of thepicturesfolder into thestudentfolder.” -
/docsmeans “now look for a folder nameddocshere and step into it.” Your final location will be/home/student/docs.
When to Use Which?
Both paths are correct, but efficient engineers know when to use each:
Use Relative Paths for Speed (Daily Operations): If you are already working inside the /home/student/ directory and you want to enter the docs folder, typing the absolute path (cd /home/student/docs) is a waste of keystrokes. Just type the relative path (cd docs). Use relative paths when you are already in the “neighborhood” of your target.
Use Absolute Paths for Safety (Automation & Scripts): If you are jumping across the server (e.g., from /var/log to /etc), an absolute path is much easier than typing cd ../../etc. More importantly, when you start writing automated scripts on Day 6, you must use Absolute paths. A script runs blindly; it doesn’t know where the user was standing when they executed it. If your script says rm -rf docs (a relative command to delete a folder), it might accidentally delete the wrong docs folder depending on where it is run. If it says rm -rf /home/student/docs, it is completely bulletproof.