If I am sshed into my Rails server, and I am in /var/www/some_app/current, it's actually a symlink pointing to (say) /var/www/some_app/releases/20201015004444. If I do a deployment, then current is replaced with a new symlink pointing to (say) /var/www/some_app/releases/20201023010000.

Neither the output of pwd nor the value of $PWD change, however. And of course my prompt doesn't change. There is no visible indicator that anything is amiss. Everything keeps humming along as if everything is great, and if I forget that I need to change to the new symlink, I won't know until something breaks or fails that I forgot to change directory.

How can I easily compare the actual current value of current with my actual current directory?

1 Answer 1


readlink -f . will tell you where you actually are, and readlink -f $PWD will tell you what the symlink is currently pointing to, even if that's not the directory you're in.

This can be easily wrapped up in a function (which you can trigger automatically if you want):

correct_current_directory() {
  [[ $(readlink -f .) = $(readlink -f $PWD) ]] || echo "Moving $PWD from $(readlink -f .) to $(readlink -f $PWD)" && cd $PWD

It does nothing if you are where you think you are, but if your symlink gets moved to another directory it courteously notifies you that it is fixing the problem. Since it lists both the old and new locations, you know exactly what's going on and can move back in that rare 0.01% of the time when you didn't want to get moved.

You must log in to answer this question.

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