[英]Behavior of cd/bash on symbolic links
假设我的主文件夹中有文件夹 ~/a/b,并且文件夹 b 包含一个名为“symlink”的“..”符号链接。 然后我在 bash 中执行以下操作:
hm@mach:~$ cd a/b/symlink
hm@mach:~/a/b/symlink$ pwd -P
/home/hm/a
hm@mach:~/a/b/symlink$ cd ..
hm@mach:~/a/b$ pwd -P
/home/hm/a/b
pwd -P 打印当前工作目录,取消引用所有符号链接。 为什么最后是工作目录/home/hm/a/b,而不是/home/hm?
根据help cd
,
Options:
-L force symbolic links to be followed: resolve symbolic
links in DIR after processing instances of `..'
-P use the physical directory structure without following
symbolic links: resolve symbolic links in DIR before
processing instances of `..'
换句话说, -L
表示使用逻辑结构,而-P
使用实际物理目录结构。
逻辑结构是这样的
$ tree a
a
└── b
└── symlink -> ..
当你去a/b/symlink
时的实际物理结构是,
a
如果你想使用真正的..
,那么你还必须使用cd -P
:
The -P option says to use the physical directory
structure instead of following symbolic links (see
also the -P option to the set builtin command);
the -L option forces symbolic links to be followed.
一个例子,
$ cd
$ cd a/b/symlink # physical location is at a/
$ cd .. # now is at a/b
$ cd symlink # goes back to a/b/symlink
$ cd -P .. # follow physical path (resolve all symlinks)
$ pwd -P # -P is optional here to show effect of cd ..
/home/sarnold
$
bash
跟踪当前的逻辑目录路径,如您的提示所示,并据此解释cd ..
。 如果您只在cd
(或pushd
)中使用这样的路径,这会使事情更加一致,代价是发生意外的事情,如果您希望命令参数中的路径(或内部命令; emacs
和vim
有)发生同样的事情他们自己的符号链接处理可配置规则,但大多数命令依赖内核来处理它)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.