Home > linux > bash > cdsymboliclinks | About

use cd -P to access the real location of the symbolic link. Using -P will change how cd .. works if you cd into a symlink. Here's an example of cd with and without -P

cd

We will acces /home/ychaouche/public_html and see what's there.

ychaouche@ychaouche-PC ~ $ cd public_html/
ychaouche@ychaouche-PC ~/public_html $ ls
total 0
lrwxrwxrwx 1 ychaouche ychaouche 31 Mar 19 15:35 STREAMING -> /home/ychaouche/CODE/STREAMING/
ychaouche@ychaouche-PC ~/public_html $

STREAMING is a symbolic link to /home/ychaouche/CODE/STREAMING. Let's access it, list its contents, then get back to the parent directory :

ychaouche@ychaouche-PC ~/public_html $ cd STREAMING ychaouche@ychaouche-PC ~/public_html/STREAMING $ ls total 12K drwxr-xr-x 2 ychaouche ychaouche 4.0K Mar 19 11:35 css drwxr-xr-x 2 ychaouche ychaouche 4.0K Mar 19 15:31 js -rw-r–r– 1 ychaouche ychaouche 1.2K Mar 19 15:31 player.html ychaouche@ychaouche-PC ~/public_html/STREAMING $ cd .. ychaouche@ychaouche-PC ~/public_html $ ls total 0 lrwxrwxrwx 1 ychaouche ychaouche 31 Mar 19 15:35 STREAMING → /home/ychaouche/CODE/STREAMING/ ychaouche@ychaouche-PC ~/public_html $ </code>

We got back to where we were, ~/public_html.

cd -P

Now with -P.

ychaouche@ychaouche-PC ~/public_html $ ls 
total 0
lrwxrwxrwx 1 ychaouche ychaouche 31 Mar 19 15:35 STREAMING -> /home/ychaouche/CODE/STREAMING/
ychaouche@ychaouche-PC ~/public_html $ cd -P STREAMING
ychaouche@ychaouche-PC ~/CODE/STREAMING $ 

Notice that the cwd isn't ~public_html/STREAMING but ~/CODE/STREAMING instead. We're kind of accessing the "real" path of the file. Now let's the contents of the directory and try to get the parent directory. Where will we land : ~/public_html or ~/CODE ?

ychaouche@ychaouche-PC ~/CODE/STREAMING $ ls
total 12K
drwxr-xr-x 2 ychaouche ychaouche 4.0K Mar 19 11:35 css
drwxr-xr-x 2 ychaouche ychaouche 4.0K Mar 19 15:31 js
-rw-r--r-- 1 ychaouche ychaouche 1.2K Mar 19 15:31 player.html
ychaouche@ychaouche-PC ~/CODE/STREAMING $ cd ..
ychaouche@ychaouche-PC ~/CODE $ 

contact : @ychaouche yacinechaouche at yahoocom


QR Code
QR Code cd (generated for current page)