Unix Software Links and Paths

I'm a little confused about how soft links work in unix. See an example.

% cd /usr/local/ % ls -la total 6 drwxr-xr-x 2 root root 512 Jan 19 15:03 . drwxr-xr-x 41 root sys 1024 Jan 20 16:24 .. lrwxrwxrwx 1 root root 38 Jan 19 15:03 java -> /otherDir/java/jdk1.6.0_17 **<- this is a soft link** % cd java **<- move to the softlink** % pwd /usr/local/java **<- the current location, say LOCATION_A** % cd /otherDir/java/jdk1.6.0_17/ **<-move to the location of the softlink** % pwd /otherDir/java/jdk1.6.0_17 **<- the new current location, say LOCATION_B** 

Isn't it a problem that although LOCATION_A LOCATION_B, they have different paths?

Is there a command (other than pwd) that will give the actual location of the file (and not just how the user goes there).

It seems to me that pwd is just the sum of user cd. NOT their current location.

+2
unix directory symlink pwd
source share
6 answers

It behaves this way for a specific purpose. If you cd to /a/b/c/d , and then cd to .. , then you really expect to be in /a/b/c . If c is a symbolic link (or a symbolic link in terms of unix, but not a soft link) that leads you to /f/g/h , with the behavior you would like to get, you will end up in /f/g , and then you (or any program) will not understand how he got there.

+3
source share

Try pwd -P . This is not "non-pwd", but it is a trick, at least on my bash 4.0.35 on Fedora 12. YMMV.

Update: even works with sh , so it seems portable.

+4
source share

You can use readlink in the current working directory to get the true directory name:

 readlink `pwd` 
+1
source share

Normally pwd should return /usr/local/java on the last line if I understand your example. But some shells have a built-in pwd , which tries to be more β€œsmart” handling symbolic links in the current working directory.

Try /bin/pwd , will you get other results?

+1
source share

realpath does what you want.

0
source share

It is impossible to absolutely get your way under any circumstances. This is a bit strange, but a variation of this (plus chroot and setuid) is sometimes used to block the process.

  $ mkdir -p / tmp / a / b
 $ cd / tmp / a / b
 $ rmdir / tmp / a / b
 $ chmod 0 / tmp / a
 $ rmdir / tmp / a
 $ ls ..
 ls: cannot open directory ..: Permission denied
 $ ls -al
 total 0
 $ pwd -P
 pwd: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
0
source share

All Articles