How to get full path of a file?

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

    In mac mentioned below line works. No need to add any fancy lines.

    > pwd filename
    
    0 讨论(0)
  • 2020-12-02 03:53

    Beside "readlink -f" , another commonly used command:

    $find  /the/long/path/but/I/can/use/TAB/to/auto/it/to/ -name myfile
    /the/long/path/but/I/can/use/TAB/to/auto/it/to/myfile
    $
    

    This also give the full path and file name at console

    Off-topic: This method just gives relative links, not absolute. The readlink -f command is the right one.

    0 讨论(0)
  • 2020-12-02 03:54

    Use readlink:

    readlink -f file.txt
    
    0 讨论(0)
  • 2020-12-02 03:55

    This works with both Linux and Mac OSX ..

     echo $(pwd)$/$(ls file.txt)
    
    0 讨论(0)
  • 2020-12-02 03:56

    The following usually does the trick:

     echo "$(cd "$(dirname "$1")" && pwd -P)/$(basename "$1")"
    
    0 讨论(0)
  • 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} \'
    }
    
    0 讨论(0)
提交回复
热议问题