Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change my pwd to the real path of a symlinked directory?

Tags:

linux

symlink

pwd

Here's a rather elementary *nix question:

Given the following symlink creation:

ln -s /usr/local/projects/myproject/ myproject 

... from my home directory /home/jvf/, entering the myproject symlink gives me a pwd /home/jfv/myproject/. Now, I would like to enter the parent directory of the directory I've symlinked to, but the cd .. command will only bring me back to my home directory /home/jfv/. Is there anyway to escape the symlink trail that I've entered, and instead have a pwd equal to the actual path of the myproject directory. That is, changing my pwd from /home/jfv/myproject/ into /usr/local/projects/myproject/?

Thanks :)

like image 688
Johan Fredrik Varen Avatar asked Feb 19 '10 19:02

Johan Fredrik Varen


People also ask

Can you cd into a symlink?

You can create a symlink to a directory that doesn't exist, but you can't then "cd" into it... the source directory still doesn't exist. :) You can, however, make a symlink to a non-existent file and then edit that the file through the symlink. The source file will appear when you save, just like editing any new file.


2 Answers

Just use -P (physical) flag:

pwd -P  cd -P .. 
like image 155
Cfr Avatar answered Sep 20 '22 06:09

Cfr


If you do the following you should be OK.

1) First you follow your symlink:

[jfv@localhost ~]$ cd myproject 

2) Now you execute the following command:

[jfv@localhost myproject]$ cd -P ./ 

3) Now, you can check your location and you will see that you are on the physical directory

[jfv@localhost myproject]$ pwd 

The output will be as follows:

/usr/local/projects/myproject 

Now, everything you do will be local and not on the symlink.

like image 37
DrupalFever Avatar answered Sep 19 '22 06:09

DrupalFever