How to get full path of a file?

后端 未结 30 3021
走了就别回头了
走了就别回头了 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
    
    0 讨论(0)
  • 2020-12-02 03:57

    For Mac OS, if you just want to get the path of a file in the finder, control click the file, and scroll down to "Services" at the bottom. You get many choices, including "copy path" and "copy full path". Clicking on one of these puts the path on the clipboard.

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

    In Mac OSX, do the following steps:

    1. cd into the directory of the target file.
    2. Type either of the following terminal commands.
    Terminal
    ls "`pwd`/file.txt"
    echo $(pwd)/file.txt
    
    1. Replace file.txt with your actual file name.
    2. Press Enter
    0 讨论(0)
  • 2020-12-02 03:59

    Usually:

    find `pwd` | grep <filename>
    

    Alternatively, just for the current folder:

    find `pwd` -maxdepth 1 | grep <filename>
    
    0 讨论(0)
  • 2020-12-02 03:59

    the easiest way I found is

    for i in `ls`; do echo "`pwd`/$i"; done
    

    it works well for me

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

    Works on Mac, Linux, *nix:

    This will give you a quoted csv of all files in the current dir:

    ls | xargs -I {} echo "$(pwd -P)/{}" | xargs | sed 's/ /","/g'
    

    The output of this can be easily copied into a python list or any similar data structure.

    0 讨论(0)
提交回复
热议问题