How to get full path of a file?

后端 未结 30 3234
走了就别回头了
走了就别回头了 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:07

    I like many of the answers already given, but I have found this really useful, especially within a script to get the full path of a file, including following symlinks and relative references such as . and ..

    dirname `readlink -e relative/path/to/file`
    

    Which will return the full path of the file from the root path onwards. This can be used in a script so that the script knows which path it is running from, which is useful in a repository clone which could be located anywhere on a machine.

    basePath=`dirname \`readlink -e $0\``
    

    I can then use the ${basePath} variable in my scripts to directly reference other scripts.

    Hope this helps,

    Dave

提交回复
热议问题