views:

112

answers:

3

Hi folks.

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 :)

+2  A: 

Just use -P (physical) flag:

pwd -P

cd -P ..
Cfr
Thanks a dozen :D
Johan Fredrik Varen
A: 

Programmatically, you would do this with the getcwd library function:

#include <unistd.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    char buf[1024*1024L];
    char *cwd;

    cwd = getcwd(buf, sizeof buf);
    if (cwd == NULL) {
        perror("getcwd");
        return 1;
    }
    printf("%s\n", cwd);
    return 0;
}
Lars Wirzenius
Thanks, I'll keep that in mind should I ever need to do this programmatically ;)
Johan Fredrik Varen
A: 

How can you force the -P option by default? I'm using zsh on Mac and I don't know how to change it.

Sridhar Sarnobat