首页 > Sysadmin > 正文

How to Navigate Up a Directory in Linux: cd .. and cd –

2026-11-02 Sysadmin

Jumping to the wrong directory happens all the time in the terminal. This guide covers four tested ways to get back to the right place quickly.

1. cd -: Jump to the previous directory

# Two cd - commands toggle between /tmp and /etc (tested on WSL2 Ubuntu 24.04, bash 5.2.x)
cd /tmp
cd /etc
cd -        # Output: /tmp
cd -        # Run it again, and you're back in /etc

The output path is the current working directory, so you can read exactly where you’re going next [1].

2. cd ..: Move up one level at a time

cd /var/log/nginx                # Inside /var/log/nginx
cd ..                            # Back to /var/log
cd /var/log/nginx/site-a/deep    # Inside /var/log/nginx/site-a/deep
cd ../..                         # Up two levels in one shot, to /var/log/nginx
cd ../../..                      # Up three levels in one shot

.. is one of the special . and .. entries in every directory; it points to the parent of the current directory. Chaining several ../ moves up multiple levels at once [2].

Common shortcuts for bouncing out of deep paths:

# Inside /a/b/c/d/e, back to /a/b
cd ../../..     # Shorthand for cd ../../../..

After every cd, the $OLDPWD variable is automatically updated, building a full path-jump history [4].

3. Use pwd to confirm your actual location

cd /var/log
pwd            # Logical path: /var/log
ln -sfn /etc /tmp/mylink
cd /tmp/mylink
pwd           # Logical: /tmp/mylink
pwd -P        # Physical: /etc (resolves the symlink)
rm /tmp/mylink

The $PWD environment variable keeps the logical path in sync when you traverse paths; /proc/self/cwd is also a kernel-maintained symlink to the current directory and can be read directly [5]. The -L (default) and -P behavior of pwd is recorded by POSIX and Linux man7 [3][4].

4. Troubleshooting cheat sheet

Symptom Cause Fix
cd - errors on first use Never switched directories before, so $OLDPWD is empty Switch to another directory first, then retry
cd .. won’t go up Already at root /; the parent is itself Run pwd to confirm the current path
Multi-layer .. gives wrong level Missed or added an extra layer Run pwd to check the current depth first
pwd shows a wrong-looking path Current path contains a symlink Use pwd -P to reveal the physical path

References

The commands in this article were tested on WSL Ubuntu (24.04) and cross-checked against Debian and general-distro documentation.