Linux cd/bash 在符号链接上的行为

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10456784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:10:45  来源:igfitidea点击:

Behavior of cd/bash on symbolic links

linuxbashfilesystemssymlink

提问by Hermann Speiche

Assume I have the folders ~/a/b in my home folder, and the folder b contains a symbolic link to '..' named 'symlink'. Then I perform the following actions in bash:

假设我的主文件夹中有文件夹 ~/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 prints the current working directory, dereferencing all symbolic links. Why is the working directory /home/hm/a/b at the end, and not /home/hm?

pwd -P 打印当前工作目录,取消引用所有符号链接。为什么最后是工作目录/home/hm/a/b,而不是/home/hm?

采纳答案by sarnold

According to help cd,

根据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 `..'

In other words, -Lmeans using the logicalstructure, whereas -Puses the actually physicaldirectory structure.

换句话说,-L意味着使用逻辑结构,而-P使用实际物理目录结构。

The logical structure is like this,

逻辑结构是这样的

$ tree a
a
└── b
    └── symlink -> ..

The actual physical structure when you go to a/b/symlinkis,

当你去的时候实际的物理结构a/b/symlink是,

a

If you want to use the real.., then you must also use cd -P:

如果要使用real..,则还必须使用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.

An example,

一个例子,

$ 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
$ 

回答by geekosaur

bashkeeps track of the logical current directory path, as shown in your prompt, and interprets things like cd ..according to that. This makes things a little more consistent if you only use such paths in cd(or pushd), at the cost of unexpected things happening if you then expect the same thing to happen with paths in command arguments (or inside commands; emacsand vimhave their own configurable rules for symlink handling, but most commands rely on the kernel to deal with it).

bash跟踪当前的逻辑目录路径,如您的提示所示,并cd ..据此解释类似的内容。如果您只在cd(or pushd) 中使用这样的路径,这会使事情更加一致,代价是发生意外的事情,如果您希望命令参数中的路径(或命令内部;emacs并且vim有自己的可配置规则)发生同样的事情用于符号链接处理,但大多数命令依赖内核来处理它)。