Bash: Resolve symlinks in your working directory

Say you have created a symlink from your home dir to another location:

$ ln -s /Applications/XAMPP/xamppfiles/htdocs ~/htdocs

Now you can easily change to the XAMPP htdocs: cd ~/htdocs and then get back home: cd ..

But how do you get to xamppfiles?

Update: Thanks to sapphirepaw, the solution is simple: cd -P htdocs/.. or to resolve your current wd: cd -P .

Less optimal methods follow:

The secret is pwd -P, which outputs your “real” working directory with symlinks resolved. By escaping this with $(...), we can include this in a cd command:

$ cd $(pwd -P)  # change working directory to the real current path.

So to get to xamppfiles from home:

$ cd htdocs $ cd $(pwd -P)/..

2 thoughts on “Bash: Resolve symlinks in your working directory

  1. says:

    A while back, I learned that `cd` itself accepts -P, so your final example can be simplified to: `cd -P htdocs/..`

    Have fun!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.