Skip to content
TaeyoungKim.dev

Linux absolute vs relative paths: Why cd logs differs from cd /logs

LinuxWritten 4 min readTaeyoungKim
LinkedInX

Yesterday cd logs worked; today it says the directory does not exist. Before retrying or adding sudo, run pwd. The folder may be fine—you may simply be looking from a different working directory. A path is determined by both its name and where the lookup starts.

Where does a relative path start?

The working directory is the starting point

The shell keeps a current working directory, and pwd prints it. In this example, user is a placeholder account name:

bash
$ pwd
/home/user/project

Linux paths form a tree rooted at /. A leading slash means start from the root. Without one, as in logs, the lookup starts at the current working directory.

From /home/user/project, logs resolves below project; /logs starts at the root and names a different location. An absolute path is not guaranteed to exist merely because its starting point is fixed.

text
/
└── home
    └── user
        └── project       ← current directory: /home/user/project
            └── logs

Here logs refers to /home/user/project/logs. If you move to /home/user, the same word refers to /home/user/logs instead.

cd logs and cd /logs have different origins

Working directoryCommandResolved target
/home/user/projectcd logs/home/user/project/logs
/home/user/projectcd ./logs/home/user/project/logs
/home/user/projectcd /logs/logs

./ explicitly means “from this directory,” so the first two commands are equivalent here. cd /logs ignores the current directory. If /logs does not exist, it fails with “No such file or directory.” An absolute path fixes the origin; it does not guarantee success.

Where are you after cd fails?

Start in project, enter its logs child, and use cd - to return to the previous location:

bash
pwd
cd logs
pwd
cd -
pwd

An illustrative run ends each line with these path components. The temporary parent prefix may differ by machine:

text
.../project
.../project/logs
.../project
.../project

If cd /logs then fails, pwd still shows .../project. The failed cd has not moved the shell. In a deployment script, stop before later commands run from the wrong directory:

bash
cd "$target_dir" || exit 1

This assumes target_dir was set earlier. Pasting it into an interactive shell can exit that session on failure; while exploring by hand, inspect pwd after cd instead.

.., ~, and - are different destinations

CommandMeaningExample from /home/user/project/logs
cd ..Parent directory/home/user/project
cd ~ or bare cdUser's home directory/home/user, if that is the home
cd -Previous working directoryDepends on the preceding move

.. expresses a filesystem relationship. - refers to shell history for the previous working directory. ~ is expanded to the user's home. They may happen to produce the same path, but they are not the same rule. Repeating cd - can toggle between two locations.

What should you check after a path error?

  1. Run pwd to see the current working directory.
  2. Check whether the input starts with /. That decides whether lookup starts at root or here.
  3. Use ls or ls -ld to check whether the target directory exists.
  4. Quote names with spaces, for example cd "project files".
  5. In scripts, test whether cd succeeded before continuing.

“No such file or directory” and “Permission denied” point to different investigations. If a path does not exist, elevated privileges do not create it. If it exists but traversal is denied, inspect the current user and directory permissions.

Key takeaways

pwd tells you where relative lookup begins. cd logs begins there; cd /logs begins at the root. After a failed cd, check pwd rather than assuming the shell moved. In scripts, stop on a failed directory change before subsequent commands run in the wrong place.

Author

TaeyoungKim

Connecting technical foundations with implementation, verification, and production decisions.

#Linux#absolute path#relative path#cd command#pwd command

Read next