5

Say I got a folder with absolute location: /tmp/abc/def, and I created a symbolic link to this folder def, in my home directory. The symbolic link is also called def. Then after I get to the def from my home directory by calling:

cd ~/def

I then want to go to the folder "abc". What should I do then?

I've search for around 20 minutes but didn't find the answer. Thanks.

1

3 Answers 3

18

You can use pwd -P to get the "real" path, so something like this would work:

cd "$(pwd -P)/.."
2
  • I didn't know this before. Thanks :)
    – songyy
    Commented Jun 6, 2012 at 16:38
  • Works great on macOS out-of-the-box, unlike the other answer with just cd .. Commented Oct 26, 2018 at 19:28
0

Type in:

cd ..

That should bring you to the parent directory of whatever your current directory is.

5
  • 2
    This solution has exactly the problem the OP wants to solve. When entering the directory via symlink, the cd .. will return to the directory that contains the symlink. Commented Jun 6, 2012 at 15:34
  • 4
    This solution works, but only in certain settings. For example, in bash set -P will enable this, but set +P will cause it to fail. In either case, cd -P .. should work. Commented Jun 6, 2012 at 16:18
  • @WilliamPursell: thanks, this method is clean and cool :)
    – songyy
    Commented Jun 6, 2012 at 16:37
  • 1
    The cd -P .. in comments here worked great in macOS out-of-the-box. Commented Oct 26, 2018 at 19:29
  • Why isn't cd -P .. the answer? cd .. has obviously been tried by anyone who googled this. Commented Oct 31, 2018 at 1:09
0

You can also use th -P flag with cd:

cd -P ..

This was pointed out above in comments, just adding this answer to give more visibility to that solution.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .