How to resolve symbolic links in a shell script

后端 未结 19 2246
死守一世寂寞
死守一世寂寞 2020-11-28 00:48

Given an absolute or relative path (in a Unix-like system), I would like to determine the full path of the target after resolving any intermediate symlinks. Bonus points for

19条回答
  •  时光说笑
    2020-11-28 01:39

    In case where pwd can't be used (e.g. calling a scripts from a different location), use realpath (with or without dirname):

    $(dirname $(realpath $PATH_TO_BE_RESOLVED))
    

    Works both when calling through (multiple) symlink(s) or when directly calling the script - from any location.

提交回复
热议问题