如何将 pwd 更改为符号链接目录的实际路径?

这里有一个相当基本的问题:

给定以下符号链接的创建:

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

... 从我的主目录 /home/jvf/,输入 我的项目符号链接给我一个 pwd /home/jfv/myproject/。现在,我想进入我已经符号链接到的目录的父目录,但是 CD.命令只会把我带回到我的主目录 /家/jfv/。是否可以转义我输入的符号链接路径,而使用与 我的项目目录的实际路径相等的 pwd。也就是说,将我的 pwd 从 /home/jfv/myproject/改为 /usr/local/Projects/myproject/

谢谢:)

19334 次浏览

Just use -P (physical) flag:

pwd -P


cd -P ..

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;
}

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.