Back to Blog
Lesson 3 of the Linux: Linux Command Line for Developers course
LinuxJuly 14, 20264 min read

Mastering Absolute and Relative Paths in Linux

Learn to navigate the Linux file system using absolute and relative paths. Master shortcuts like .. and ~ to speed up your terminal workflow today.

linuxcommand linebashfile systempaths

Previously in this course, we covered Navigating the File System: A Linux Command Line Guide, where you learned how to use pwd and cd to orient yourself within the Linux hierarchy. Now, we'll level up your navigation skills by mastering file paths, the precise language we use to tell the shell exactly where a file or directory lives.

Understanding Absolute Paths

An absolute path defines the location of a file or directory starting from the root of the file system, denoted by a forward slash (/). Because it starts from the absolute top of the tree, an absolute path is always the same, regardless of your current working directory.

Think of an absolute path like a global GPS coordinate. Whether you are in your home folder or deep inside a system directory, /var/log/syslog will always point to the same file.

Example: If you want to move to the etc directory, which contains configuration files, you use: cd /etc

No matter where you are currently, running that command takes you directly to the system-wide configuration folder.

Navigating with Relative Paths

A relative path defines the location of a file or directory based on your current working directory. Instead of starting from the root, you describe the route from where you are standing right now.

If your current directory is /home/user and you want to enter the documents folder inside it, you don't need to type the full path. You simply type: cd documents

The shell understands this as "look in the current directory for a folder named documents."

Essential Shortcuts for Faster Navigation

To become proficient in the shell, you must memorize these three critical shortcuts. They save you from typing long, repetitive strings.

ShortcutMeaningUse Case
.Current directorySpecifying a file in the folder you are in.
..Parent directoryMoving one level up in the hierarchy.
~User home directoryJumping straight to your personal folder.

Using the .. Shortcut

The .. (dot-dot) is perhaps the most used shortcut in Linux. It represents the directory "above" your current one. If you are in /home/user/projects and need to go to /home/user, simply run: cd ..

Using the ~ Shortcut

The tilde (~) is a shorthand for your home directory (e.g., /home/username). It’s a massive time-saver. Instead of typing cd /home/student, you can simply run: cd ~

Worked Example: Putting It Together

Let's imagine our project structure. We have a home directory, a web-server folder, and a logs sub-folder.

  1. Start in your home directory: cd ~
  2. Move into the project directory: cd web-server
  3. Move into the logs sub-folder: cd logs
  4. Now, move back to the web-server folder (the parent): cd ..
  5. Finally, jump back to your home directory from anywhere: cd ~

Hands-on Exercise

Open your terminal and follow these steps to practice path navigation:

  1. Use pwd to confirm you are in your home directory.
  2. Create a nested directory structure: mkdir -p test/subdir/deeper.
  3. Move into the deeper directory using a relative path: cd test/subdir/deeper.
  4. Use cd ../.. to move two levels up.
  5. Use pwd to confirm you are now back in the test directory.

Common Pitfalls

  • Forgetting the root slash: If you type cd etc instead of cd /etc, the shell will look for an etc folder inside your current directory. It will likely return a "No such file or directory" error.
  • Case Sensitivity: Linux is case-sensitive. cd Documents is not the same as cd documents. Always check your spelling and casing.
  • Leading spaces: Accidentally typing a space before a path can cause the shell to misinterpret your command.

FAQ

Q: How do I know if I should use an absolute or relative path? A: Use absolute paths in scripts or cron jobs where the location must be guaranteed. Use relative paths for manual, daily navigation because they are faster to type.

Q: Can I use ~ in a path? A: Yes. cd ~/web-server/logs will take you to that specific path regardless of your current location.

Recap

You've learned that absolute paths are fixed, global addresses starting with /, while relative paths are "directions" from your current spot. By leveraging .. to move up and ~ to jump home, you've significantly reduced the keystrokes required to manage your environment.

Up next: Listing and Inspecting Files

Similar Posts