How to resolve symbolic links in a shell script

后端 未结 19 2101
死守一世寂寞
死守一世寂寞 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:36

    Here's how one can get the actual path to the file in MacOS/Unix using an inline Perl script:

    FILE=$(perl -e "use Cwd qw(abs_path); print abs_path('$0')")
    

    Similarly, to get the directory of a symlinked file:

    DIR=$(perl -e "use Cwd qw(abs_path); use File::Basename; print dirname(abs_path('$0'))")
    

提交回复
热议问题