簡體   English   中英

cd/bash 在符號鏈接上的行為

[英]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 )中使用這樣的路徑,這會使事情更加一致,代價是發生意外的事情,如果您希望命令參數中的路徑(或內部命令; emacsvim有)發生同樣的事情他們自己的符號鏈接處理可配置規則,但大多數命令依賴內核來處理它)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM