How to get full path of a file?

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

    This will work for both file and folder:

    getAbsolutePath(){
        [[ -d $1 ]] && { cd "$1"; echo "$(pwd -P)"; } || 
        { cd "$(dirname "$1")" || exit 1; echo "$(pwd -P)/$(basename "$1")"; }
    }
    
    0 讨论(0)
  • 2020-12-02 03:43

    You can save this in your shell.rc or just put in console

    function absolute_path { echo "$PWD/$1"; }
    alias ap="absolute_path"
    

    example:

    ap somefile.txt
    

    will output

    /home/user/somefile.txt
    
    0 讨论(0)
  • 2020-12-02 03:44

    You could use the fpn (full path name) script:

    % pwd
    /Users/adamatan/bins/scripts/fpn
    
    % ls
    LICENSE   README.md fpn.py
    
    % fpn *
    /Users/adamatan/bins/scripts/fpn/LICENSE
    /Users/adamatan/bins/scripts/fpn/README.md
    /Users/adamatan/bins/scripts/fpn/fpn.py
    

    fpn is not a standard Linux package, but it's a free and open github project and you could set it up in a minute.

    0 讨论(0)
  • 2020-12-02 03:47
    find $PWD -type f | grep "filename"
    

    or

    find $PWD -type f -name "*filename*"
    
    0 讨论(0)
  • 2020-12-02 03:47

    In a similar scenario, I'm launching a cshell script from some other location. For setting the correct absolute path of the script so that it runs in the designated directory only, I'm using the following code:

    set script_dir = `pwd`/`dirname $0`
    

    $0 stores the exact string how the script was executed.

    For e.g. if the script was launched like this: $> ../../test/test.csh, $script_dir will contain /home/abc/sandbox/v1/../../test

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

    Create a function like the below (echoes the absolute path of a file with pwd and adds the file at the end of the path:

    abspath() { echo $(pwd "$1")/"$1"; }
    

    Now you can just find any file path:

    abspath myfile.ext
    
    0 讨论(0)
提交回复
热议问题