How to get full path of a file?

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

    You may use this function. If the file name is given without relative path, then it is assumed to be present in the current working directory:

    abspath() { old=`pwd`;new=$(dirname "$1");if [ "$new" != "." ]; then cd $new; fi;file=`pwd`/$(basename "$1");cd $old;echo $file; }
    

    Usage:

    $ abspath file.txt
    /I/am/in/present/dir/file.txt
    

    Usage with relative path:

    $ abspath ../../some/dir/some-file.txt
    /I/am/in/some/dir/some-file.txt
    

    With spaces in file name:

    $ abspath "../../some/dir/another file.txt"
    /I/am/in/some/dir/another file.txt
    

提交回复
热议问题