Shell

如何判斷我是否真的在命令行的符號連結位置?

  • December 19, 2016

假設我有一個文件夾:

cd /home/cpm135/public_html

並建立一個符號連結

ln -s /var/lib/class .

後來,我在那個目錄中:

cd /home/cpm135/public_html/class

pwd告訴我我在/home/cpm135/public_html/class

有什麼方法可以知道我“真的”在/var/lib/class嗎?謝謝

根據您的pwd命令的配置方式,它可能預設顯示將顯示符號連結位置的邏輯工作目錄(由 輸出),或忽略符號連結並顯示“真實”目錄的物理工作目錄(由 輸出)。pwd -L``pwd -P

有關完整資訊,您可以

file "$(pwd -L)"

在符號連結中,這將返回

/path/of/symlink: symbolic link to /path/of/real/directory

請注意,這pwd實際上是一個內置的 shell。根據您的 shell 及其配置,結果可能會發生變化。對於更便攜的解決方案,您應該使用/bin/pwd. 手冊頁的片段:

NAME
      pwd - print name of current/working directory

SYNOPSIS
      pwd [OPTION]...

DESCRIPTION
      Print the full filename of the current working directory.

      -L, --logical
             use PWD from environment, even if it contains symlinks

      -P, --physical
             avoid all symlinks

      --help display this help and exit

      --version
             output version information and exit

      If no option is specified, -P is assumed.

      NOTE:  your  shell  may  have  its  own  version of pwd, which usually supersedes the version described here.  Please refer to your shell's documentation for
      details about the options it supports.

通常,您可以使用readlink -f. readlink -f .類似於pwd -P.

引用自:https://unix.stackexchange.com/questions/331208