How to get full path of a file?

后端 未结 30 3344
走了就别回头了
走了就别回头了 2020-12-02 03:22

Is there an easy way I can print the full path of file.txt ?

file.txt = /nfs/an/disks/jj/home/dir/file.txt

The

30条回答
  •  感情败类
    2020-12-02 04:03

    echo $(cd $(dirname "$1") && pwd -P)/$(basename "$1")
    

    This is explanation of what is going on at @ZeRemz's answer:

    1. This script get relative path as argument "$1"
    2. Then we get dirname part of that path (you can pass either dir or file to this script): dirname "$1"
    3. Then we cd "$(dirname "$1") into this relative dir
    4. && pwd -P and get absolute path for it. -P option will avoid all symlinks
    5. After that we append basename to absolute path: $(basename "$1")
    6. As final step we echo it

提交回复
热议问题