How to get full path of a file?

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

    This worked pretty well for me. It doesn't rely on the file system (a pro/con depending on need) so it'll be fast; and, it should be portable to most any *NIX. It does assume the passed string is indeed relative to the PWD and not some other directory.

    function abspath () {
       echo $1 | awk '\
          # Root parent directory refs to the PWD for replacement below
          /^\.\.\// { sub("^", "./") } \
          # Replace the symbolic PWD refs with the absolute PWD \
          /^\.\//   { sub("^\.", ENVIRON["PWD"])} \
          # Print absolute paths \
          /^\//   {print} \'
    }
    

提交回复
热议问题