linux - 当您涉及软链接(soft link)时, "cd .."和 "pwd"究竟意味着什么?

标签 linux unix filesystems symlink

<分区>

请参阅下面的 Unix 命令。 当我们处理软链接(soft link)时,从每个目录到根目录可以有多个路径。 那么这种情况下,pwdcd ..是怎么计算的呢?这意味着目录路径不再是无状态的,对吧?

$ cd ~
$ mkdir a b
$ cd a
$ ln -s ~/b b
$ cd b
$ pwd
/home/myuser/a/b

$ cd ..
$ pwd
/home/myuser/a

最佳答案

看这个例子:

[myuser@test ~]$ pwd
/home/myuser
[myuser@test ~]$ mkdir a b
[myuser@test ~]$ cd a
[myuser@test a]$ ln -s ~/b b
[myuser@test a]$ cd b
[myuser@test b]$ pwd
/home/myuser/a/b
[myuser@test b]$ pwd -P 
/home/myuser/b
[myuser@test b]$ echo $$ 
2432
[myuser@test b]$ ls -l /proc/2432/cwd
lrwxrwxrwx 1 myuser myuser 0 Oct  4 04:10 /proc/2432/cwd -> /home/myuser/b
[myuser@test b]$ 
[myuser@test b]$ 
[myuser@test b]$ pwd
/home/myuser/a/b
[myuser@test b]$ cd -P .. 
[myuser@test ~]$ pwd
/home/myuser
[myuser@test ~]$ 
[myuser@test ~]$ env | grep "PWD"
PWD=/home/myuser
OLDPWD=/home/myuser/a/b

请参阅 bash 手册中的选项 -P 到 cd:

-P      If set, the shell does not follow symbolic links when executing commands such  as  cd  that
                  change  the  current  working directory.  It uses the physical directory structure instead.
                  By default, bash follows the logical chain of directories when  performing  commands  which
                  change the current directory.

如您所见,内核保留的当前目录是您的真实目录(/proc/2432/cwd ->/home/myuser/b)但是 bash 可以做任何想做的事情,是否遵循符号链接(symbolic link), 因为 cd 是 bash 内部命令。

关于linux - 当您涉及软链接(soft link)时, "cd .."和 "pwd"究竟意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26190114/

相关文章:

linux - 如何在 ffmpeg 中对视频轨道进行 md5(仅)

c - 首次输出到 stdout 后特定子进程的用户 CPU 时间

c - 多个 fork() 并发

algorithm - 列出给定根目录的文件系统中的所有重复文件。

c++ - 系统 ("rm x.*") 与 unlink() 的性能?

java - Maven 连接问题

linux - 嵌套最深的可写文件夹?

linux - 使用不同用户进行 Subversion 提交后操作

linux - 复制后提取 tar 文件但未成功

c# - 在C#程序中访问linux文件系统