Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 什么是;cd.“;及;pwd“;真的是说当你有软链接的时候?_Linux_Unix_Filesystems_Symlink - Fatal编程技术网

Linux 什么是;cd.“;及;pwd“;真的是说当你有软链接的时候?

Linux 什么是;cd.“;及;pwd“;真的是说当你有软链接的时候?,linux,unix,filesystems,symlink,Linux,Unix,Filesystems,Symlink,请参阅下面的Unix命令。 当我们处理软链接时,每个目录可以有多个根路径。 那么在这种情况下,pwd和cd..是如何计算的呢?这意味着目录路径不再是无状态的,对吗 $ cd ~ $ mkdir a b $ cd a $ ln -s ~/b b $ cd b $ pwd /home/myuser/a/b $ cd .. $ pwd /home/myuser/a 命令总是根据软链接后面的实际(已解析)目录计算。 当你这样做的时候 $ cd b ; you end up in the dire

请参阅下面的Unix命令。 当我们处理软链接时,每个目录可以有多个根路径。 那么在这种情况下,
pwd
cd..
是如何计算的呢?这意味着目录路径不再是无状态的,对吗

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

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

命令总是根据软链接后面的实际(已解析)目录计算。 当你这样做的时候

$ cd b   ; you end up in the directory pointed by b
此处的任何命令都是基于此新位置解析的

请看此示例:

[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.
如您所见,内核保留的当前dir是您的真实dir(/proc/2432/cwd->/home/myuser/b),但是bash可以做任何事情,不管是否遵循符号链接,
因为cd是bash内部命令

这类问题将是关于主题的。