Linux

通過命令行找出符號連結目標

  • March 31, 2020

假設我設置了一個符號連結:

ln -s /root/Public/mytextfile.txt /root/Public/myothertextfile.txt

有沒有辦法查看myothertextfile.txt使用命令行的目標是什麼?

使用-f標誌列印規範化版本。例如:

readlink -f /root/Public/myothertextfile.txt

來自man readlink

-f, --canonicalize
     canonicalize by following every symlink in every component of the given name recursively; all but the last component must exist

readlink 是您想要的命令。您應該查看該命令的手冊頁。因為如果你想跟踪到實際文件的符號連結鏈,那麼你需要 -e 或 -f 開關:

$ ln -s foooooo zipzip   # fooooo doesn't actually exist
$ ln -s zipzip zapzap

$ # Follows it, but doesn't let you know the file doesn't actually exist
$ readlink -f zapzap
/home/kbrandt/scrap/foooooo

$ # Follows it, but file not there
$ readlink -e zapzap

$ # Follows it, but just to the next symlink
$ readlink zapzap
zipzip

引用自:https://serverfault.com/questions/76042